ReactiveObjC
ReactiveObjC copied to clipboard
RACMulticastConnection and error handling
Hello, Using RACMulticastConnection it's quite easy to cache the result of a network request. However I'm having trouble in handling gracefully the following scenario: If the signal completes with error the first new subscription should trigger again the signal, all this in a thread safe manner.
Thanks.
Hi @raduraducu—it sounds like you may want something like shareWhileActive
. It isn't included as a core operator in ReactiveObjC, but should give you similar behavior. It is available in this gist https://gist.github.com/jaredru/5540a15c5a1bdcc31787.
In the scenario you've described, this will have the behavior of interrupting all subscriptions when an error occurs. However, if a new subscription occurs following the error, the request will be retried.
I've looked at shareWhileActive
and it does solve half of the problem. What I want to achieve is something like this:
__weak typeof(self) welf = self;
return [[[[RACSignal zip:@[RACObserve(self, completeResult), RACObserve(self, completing)]]
flattenMap:^id(RACTuple *value) {
__strong typeof(welf) swelf = welf;
BOOL completing = [value.second boolValue];
if (completing) {
return [RACObserve(swelf, completeResult) skip:1];
}
else {
if (!self.completeResult || [self.completeResult isKindOfClass:NSError.class]) {
[self completeAgain];
return [RACObserve(swelf, completeResult) skip:1];
}
else {
return RACObserve(swelf, completeResult);
}
}
}] subscribeOn:[RACScheduler mainThreadScheduler]] take:1];
The setting of completing
and completeResult
also happens on the same (main) scheduler to take care of synchronization issues. Can this be done more nicely?