我在ReactiveCocoa和Octokit.objC(github库)之上构建应用程序。作为我努力的一部分,我使用Octokits ReactiveCocoa信号来访问需要身份验证的资源。上一个问题' Retrying an asynchronous operation using ReactiveCocoa'做了很好的工作,涵盖了用户想要重试异步操作的情况。的一次即可。我试图弄清楚如何处理您可能想要重试几次次的情况。
在我的特定情况下,如果身份验证失败,我想要询问用户的凭据。我会要求用户提供他们的凭证几次(2或3),然后如果他们失败则停止,或者我们会一直询问他们的凭据,直到他们成功为止。
任何帮助将不胜感激。谢谢 - AYAL
答案 0 :(得分:2)
有一个名为-retry:
的运算符接受count参数。如果将此运算符应用于信号,并且该信号返回错误,则会在收到错误时重新订阅该信号(最多达指定的次数)。所以你需要的是一个信号,当订阅时,会提示用户提供凭据。
@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]];