我想在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输入信号并稍后手动触发信号。我这样做了吗?
答案 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)