ReactiveCocoa signal to receive data while observing authentication status

Quite new to ReactiveCocoa, I am trying to create a signal that asynchronously fetches some resource from the remote API that the client needs to authenticate to first. Authentication is handled by first getting the token from the API and then passing it through some custom HTTP header for each subsequent request. However, a custom header can be set after the fetchResource signal is signed, which in the current situation results in an unauthenticated request. I could probably construct the request in the subscribeNext self.authenticationStatus block, thereby ensuring that the token is set, but how can I handle the location of the signal then?

- (RACSignal *)fetchResource
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSURLRequest *request = [self.requestSerializer
                                 requestWithMethod:@"GET"
                                 URLString:[[NSURL URLWithString:@"resource" relativeToURL:self.baseURL] absoluteString]
                                 parameters:nil error:nil];
        NSURLSessionDataTask *task = [self dataTaskWithRequest:request
                                      completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            } else {
                [subscriber sendNext:responseObject];
                [subscriber sendCompleted];
            }
        }];

        // Actually trigger the request only once the authentication token has been fetched.
        [[self.authenticationStatus ignore:@NO] subscribeNext:^(id _) {
            [task resume];
        }];

        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
}

      

+3


source to share


1 answer


- (RACSignal *)fetchTokenWithCredentials:(Credentials *)credentials
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

        // Fetch the token and send it to `subscriber`.
        Token *t = ... ;
        [subscriber sendNext:t];

        return nil;

    }];
}

- (RACSignal *)fetchResourceWithToken:(Token *)token
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {

        // Use `token` to set the request header. Then fetch
        // the resource and send it to `subscriber`. Basically
        // this part is what you already have.
        Resource *r = ... ;
        [subscriber sendNext:r];

        return nil;

    }];
}

      

In your view controller, enter modal authentication if you don't have a valid token. When the user clicks the submit button, do the following:



- (IBAction)handleAuthenticationSubmit:(id)sender
{
    Credentials *c = ... ;
    RACSignal *resourceSignal = [[[self fetchTokenWithCredentials:c]
            flattenMap:^(Token *t) {

                return [self fetchResourceWithToken:t];

            }]
            deliverOn:RACScheduler.mainThreadScheduler];

    [self rac_liftSelector:@selector(receiveResource:) withSignals:resourceSignal, nil];
}

- (void)receiveResource:(Resource *)resource
{
    [self.delegate authenticationController:self didReceiveResource:resource];
}

      

+2


source







All Articles