May 23, 2013

Simplify delivering RACSignals on the main thread

One of the common things you’ll do when working with ReactiveCocoa is ensure that signals are delivered on the main thread. This is important when sending signals to user interface elements which (in general on OS X and always on iOS) must be delivered on the main thread.

Compare:

RACSignal *zoomSignal = [RACObserve(self, zoom) distinctUntilChanged];
RAC(self, rulerView.tickFrequency) = [[zoomSignal map:^NSNumber *(NSNumber *zoom) {
    return @(4.f * zoom.floatValue);
}] deliverOn:[RACScheduler mainThreadScheduler]];

To:

RACSignal *zoomSignal = [RACObserve(self, zoom) distinctUntilChanged];
RAC(self, rulerView.tickFrequency) = [[zoomSignal map:^NSNumber *(NSNumber *zoom) {
    return @(4.f * zoom.floatValue);
}] deliverOnMainThread];

It’s not a huge saving, but when you’re dealing with complex compositions, I’ve found it does make a difference to a signal’s readability.

Source: http://gist.github.com/tonyarnold/5631849