-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
I am working against a backend which uses OAuth. I use a short-lived access token to request resources. When the token has expired I will receive an error response from the server. When receiving that error I must use a refresh token to send a request for a new access token. When I have the new token I can repeat the resource request.
My API calls are all RACSignals which I subscribe to to fetch resources. My problem is how to prevent the error received from the expired token request to propagate to the subscriber. In stead I want to intercept the error and send a new request to refresh the token and then use the new token to repeat the original request.
My current implementation looks something like this:
- (RACSignal *)fetchResource {
RACSignal *requestSignal = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
// Do network request
return nil;
}];
return [self tokenValidationWrapper:requestSignal];
}
- (RACSignal *)tokenValidationWrapper:(RACSignal *)requestSignal {
[requestSignal subscribeError:^(NSError *error) {
if (/*Error due to expired token*/) {
[[self refreshToken] subscribeNext:^(id x) {
// Send original request
// Obviously this does not work because requestSignal has been terminated at this point
[requestSignal subscribeNext:^(id x) {
}];
} error:^(NSError *err) {
// Send error to subscriber of requestSignal
}];
} else {
// Send error to subscriber of requestSignal
}
}];
return [requestSignal replayLazily];
}
What would be the preferred way to implement this sort of conditional signal?