coolly
coolly

Reputation: 353

How to chain signal producers or sequence of signal producers using reactive cocoa 5 or 6?

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.

Upvotes: 0

Views: 1431

Answers (1)

MeXx
MeXx

Reputation: 3357

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.

Upvotes: 6

Related Questions