RAC 4.0 How to chain SignalProducers correctly?

767 views Asked by At

I have a view model with a few different functions that look like:

func somethingSignal() -> SignalProducer<Void, NSError>
{
    return SignalProducer {
        sink, disposable in

        sink.sendNext(blabla)
        sink.sendCompleted()
    }
}

Now, these signals need to be run in sequence - one cannot start before the preivous has been Completed. I therefore have another function called something like:

func setup() -> SignalProducer<Void, NSError>
{
    return somethingSignal()
         .then(somethingSignal2())
         .then(somethingSignal3())
}

I was under the impression that then is the function to use for this sort of behaviour. Signal3 shouldn't begin until Signal2 has completed, which shouldn't start until Signal1 has completed.

The function that calls setup has the start() call.

Where am I going wrong with this?

1

There are 1 answers

0
NachoSoto On

That looks correct!

Alternatively, you can concatenate all the signals:

SignalProducer<SignalProducer<(), NSError>, NSError>(values: [
    somethingSignal(),
    somethingSignal2(),
    somethingSignal3()
])
    .flatten(.Concat)

Since your type is Void you probably don't care about the values emitted. If you do, however, note that this has slightly different semantics: it will emit values from all the signals, unlike then.