简体   繁体   中英

How do I merge a Signal and a SignalProducer into a Signal in ReactiveSwift?

How do I merge a Signal and a SignalProducer into a Signal in ReactiveSwift?

Below is what I have so far. I can't figure out how to get a Signal on line 3.

let (signalA, observerA) = Signal<String, NoError>.pipe()
let signalProducer = /* some signalproducer */
let signalFromSignalProducer == /* how to get a signal from signalProducer here??? */
let mergedSignal = Signal.merge([signalA, signalFromSignalProducer])

I've looked at this startWithSignal method

 public func startWithSignal(_ setup: (ReactiveSwift.Signal<Value, Error>, Disposable) -> Swift.Void)

but it takes a closure. Why can't it just return a signal? I don't want to have to create another nested closure for every signalProducer i need the signal from. What.

I'm new to ReactiveSwift and honestly this is the most confusing framework I've ever came across

Reactive-Swift has a start overload that takes an observer as a parameter , so I think you can achieve the same effect as merge by calling signalProducer.start(observerA) . That should send all of the produced signal's events through signalA .

As for why it's done this way: startWithSignal takes a closure so that you can wire up the produced signal before before it starts sending any events. If it just returned a signal, it would be possible to miss events in between the time you call start and the time you hook it up, depending on the producer in question. This answer gives an example of this.

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