简体   繁体   中英

ReactiveCocoa create a signal producer that can emit manually

I want to model the following scenario using ReactiveCocoa in swift.

class A{
   let flagSignalProducer = A Signal Producer
   someSignal.takeUntil(a signal that I can trigger manually which is created out of flagSignalProducer).subscribeNext{ (_) in

   }
}

How can I create the Signal Producer and use the signal as takeUntil input signal and trigger the signal later manually. Am I doing it right way?

to make a signal that can emit manually you can use Signal<(), NoError>.pipe() to get a reference to the signal's "observer", which will allow you to manually send events on the signal.

let (flagSignal, flagObserver) = Signal<(), NoError>.pipe()
someSignal.takeUntil(flagSignal).observeNext { _ in

}

//somewhere else, trigger the signal manually with the Observer
flagObserver.sendNext(())
flagObserver.sendCompleted() //complete the signal and free memory 

and if you need to convert your flagSignal to a SignalProducer for some reason, there is a SignalProducer constructor for that,

let flagSignalProducer = SignalProducer(signal: flagSignal)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM