Try to use ReativeSwift on my project, but something not perform well I have check many times, cant find out what's wrong. Everything is right, and it just not called.
class MSCreateScheduleViewModel: NSObject {
var scheduleModel = MSScheduleModel()
var validateAction: Action<(), Bool, NoError>!
override init() {
super.init()
validateAction = Action(execute: { (_) -> SignalProducer<Bool, NoError> in
return self.valiateScheduleModel()
})
validateAction.values.observeValues { (isok) in
print("isok??") //this line not called
}
validateAction.values.observeCompleted {
print("completed") //this line not called
}
}
func valiateScheduleModel() -> SignalProducer<Bool, NoError> {
let (signal, observer) = Signal<Bool, NoError>.pipe()
let signalProducer = SignalProducer<Bool, NoError>(_ :signal)
observer.send(value: true) //this line called
observer.sendCompleted() //this line called
return signalProducer
}
}
When you create a SignalProducer
by wrapping an existing signal as you do in valiateScheduleModel
, the producer observes the signal when it is started and forwards the events. The problem in this case is that the signal completes before the producer is ever returned from the function and started, and so no events are forwarded.
If you want to create a producer that immediately sends true
and completes when it is started, then you should do something like this:
func valiateScheduleModel() -> SignalProducer<Bool, NoError> {
return SignalProducer<Bool, NoError> { observer, lifetime in
observer.send(value: true)
observer.sendCompleted()
}
}
The closure will not be executed until the producer is started, and so the Action
will see the events.