简体   繁体   中英

Authenticating with ReactiveCocoa

I'm building an app on top of ReactiveCocoa and Octokit.objC (github library). As part of my effort I'm using Octokits ReactiveCocoa signals to access resources that require authentication. A previous question ' Retrying an asynchronous operation using ReactiveCocoa ' does a nice job covering the case where the user wants to 'retry an asynchronous operation' once . I'm trying to figure out how to handle the case where you might want to retry several times.

In my specific case if authentication fails I want to go ask the user for their credentials. I'll either ask the user for their credentials a few times (2 or 3) and then halt if they fail or I'll just keep asking them for their credentials until they succeed.

Any help would be appreciated. Thanks - AYAL

There is an operator called -retry: which accepts a count parameter. If you apply this operator to a signal, and that signal returns an error, it will re-subscribe to the signal (up to the specified number of times) when the error is received. So what you need is a signal that, when subscribed to, prompts the user for credentials.

@weakify(self);
RACSignal *requestCredentials = [RACSignal defer:^{

    @strongify(self);

    // (Prompt the user for credentials.)

    if (successful)
    {
        self.cachedCredentials = credentials;
        return [self authenticate:credentials];
    }
    else
    {
        return [RACSignal error:[[MyError alloc] init]];
    }

}];

// We try to authenticate using the cached credentials (the
// `-authenticate:` method returns a signal that attempts
// authentication when it is subscribed to). If the initial
// attempt to authenticate fails, we try 3 times to get the
// user to enter the correct credentials.
return [[self authenticate:self.cachedCredentials]
    catchTo:[requestCredentials retry:3]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM