I am pretty much noob in ReactiveCocoa/ReactiveSwift. I have two SignalProducers. If first SignalProducer returns nil, then I want to execute second Signal Producer otherwise not. I read the documentation, but I am not sure which syntax helps me to work something like this. Any help is highly appreciated.
Asked
Active
Viewed 1,357 times
0
-
What do you mean with "returns nil"? Do you mean that the first SignalProducer sends an Event with value nil? Or do you mean it does not send an Event at all and then terminates? – MeXx Sep 13 '17 at 13:28
-
first producer sends an event with the value nil @MeXx – coolly Sep 14 '17 at 15:03
1 Answers
6
Ok, so you want to take values from the first SignalProducer as long as these values are not nil. Then, you want to take values from the second SignalProducer. If phrased this way, it already tells you which operators you need: take(while:)
and then
:
let producerA: SignalProducer<Int?, NoError>
let producerB: SignalProducer<Int?, NoError>
...
producerA
.take(while: { $0 != nil })
.then(producerB)
The take(while:)
operator will just forward all events as long as the given block returns true. So in this case, as soon as an event is nil, the block returns false and the resulting SignalProducer completes.
The then
operator also forwards events from producerA
until producerA
completes, at which point producerB
is started and now events from producerB
are forwarded.

MeXx
- 3,357
- 24
- 39