ReactiveCocoa信号,用于在观察认证状态时获取数据



对ReactiveCocoa相当陌生,我正在尝试构建一个信号,该信号可以从客户端必须首先对其进行身份验证的远程API异步获取一些资源。身份验证是通过首先从API获取令牌来处理的,然后通过一些自定义HTTP头将其传递给每个后续请求。但是,自定义报头可能在订阅fetchResource信号之后设置,这在当前情况下会导致未经身份验证的请求。我想我可以在self的subscribeNext块中构建请求。authenticationStatus,从而确保令牌将被设置,但是我如何处理信号的处置呢?

- (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];
        }];
    }];
}
- (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;
    }];
}

在你的视图控制器中,如果你没有一个有效的令牌,呈现模态认证对话框。当用户点击"提交"按钮时,执行如下操作:

- (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];
}

相关内容

  • 没有找到相关文章

最新更新