所以我正在处理使用cookie身份验证的网络。如果我获得未授权的401或406,那么我知道cookie已过期(对于1 HR有效),因此我使用存储的令牌登录以获取新的cookie。一旦我这样做,我想再次尝试呼叫,所以我在块上进行递归调用。在一个未知的案例中,如果我在成功刷新cookie后继续获得401或406,它将无限递归。我只想再试一次。我该如何阻止它?
我尝试在第一次调用后将requestID
设置为-1,并在if(requestID == -1)
不递归的if语句中设置。但由于requestID是一个块变量,它会抛出异常"变量不可分配(缺少__block类型说明符。
+ (void)fullRequestWithBlock:(void (^)(NSDictionary *fullRequest, NSError *error))block forID: (NSString*) requestID forUID: (NSString *) userID{
NSString *pathSuffix = [NSString stringWithFormat:@"GetRequestInfo?requestId=%@&userId=%@", requestID, userID];
[[AFAppAPIClient sharedClient] getPath:pathSuffix parameters:[NSDictionary dictionaryWithObject:@"false" forKey:@"include_entities"] success:^(AFHTTPRequestOperation *operation, id JSON) {
id innerJSON = [JSON objectForKey:@"d"];
//NSStringEncoding encoding;
NSMutableDictionary *requestData = [[NSMutableDictionary alloc] initWithDictionary:innerJSON];
[requestData removeObjectForKey:@"requestor"];
[requestData removeObjectForKey:@"shortDesc"];
[requestData removeObjectForKey:@"fullDesc"];
if (block) {
block([NSDictionary dictionaryWithDictionary:fullDescription], nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if ([[[error localizedDescription] substringFromIndex:[[error localizedDescription] length] - 3] isEqualToString:@"406"])
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *storedToken = [userDefaults stringForKey:@"storedToken"];
[Login tokenLoginIDWithBlock:^(NSString *userID, NSError *error)
{
if (!error)
{
NSLog(@"Reaunthenciated User with ID: %@", userID);
[self fullRequestWithBlock:^(NSDictionary *requests, NSError *error) {
if (!error) {
if (block) {
block([NSDictionary dictionaryWithDictionary:requests], nil);
}
}
else{
block([NSDictionary dictionary], error);
}
} forID:requestID forUID:userID];
}
else{
block([NSDictionary dictionary], error);
}
} forToken:storedToken];
}
}
else if (block) {
block([NSDictionary dictionary], error);
}
}];
}