Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 908
Tony Arnold Avatar asked Jan 21 '26 23:01

Tony Arnold


2 Answers

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.)

like image 115
erikprice Avatar answered Jan 24 '26 21:01

erikprice


Looks like you're safe not to return, technically. After erring out, RACSubscriber (assuming this is subscribed via subscribeNext: and/or the subscription mechanics match this, which is probably safe), the subscription disposes of itself, which will lead to clearing out the next, error, and completion blocks. So you should be fine without the return.

like image 45
Ash Furrow Avatar answered Jan 24 '26 21:01

Ash Furrow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!