Search code examples
objective-creactive-cocoa

Returning necessary with sending an error to RACSubscriber?


return [RACSignal create:^(id <RACSubscriber> subscriber) {
    if (someError) {
        [subscriber sendError:documentCreationError];
        return;
    }

    [subscriber sendNext:nil];
    [subscriber sendCompleted];
}];

Given the example above, is the return; necessary after sending the subscriber an error, or does execution within this block stop immediately?


Solution

  • Given the example above, is the return; necessary after sending the subscriber an error, or does execution within this block stop immediately?

    Given the exact example code above, the return is not necessary. But it's not true that execution within this block stops immediately. It's much safer to use the return, in case you or someone else later adds some code that may have a side effect:

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        if (someError) {
            [subscriber sendError:documentCreationError];
        }
    
        // This message will be printed even if an error is sent above:
        NSLog(@"Do something that could have side effects.");
    
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        return nil;
    }];
    

    (This is why it's better to keep side effects out of signals, but it's impossible to enforce this with Obj-C and ReactiveCocoa.)