ReactiveCocoa创建一个可以手动发射的信号生成器

时间:2016-04-25 10:31:26

标签: swift reactive-cocoa reactive-cocoa-4

我想在swift中使用ReactiveCocoa建模以下场景。

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

   }
}

如何创建Signal Producer并将信号用作takeUntil输入信号并稍后手动触发信号。我这样做了吗?

1 个答案:

答案 0 :(得分:1)

制作一个可手动发射的信号,您可以使用Signal<(), NoError>.pipe()来获取信号&#34;观察者&#34;的参考,这将允许您手动发送信号上的事件

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 

如果您因某种原因需要将flagSignal转换为SignalProducer,则可以使用SignalProducer构造函数,

let flagSignalProducer = SignalProducer(signal: flagSignal)