【问题标题】:iOS: AFNetworking: repeat a block if token expirediOS:AFNetworking:如果令牌过期,则重复一个块
【发布时间】:2013-06-15 21:31:03
【问题描述】:

我正在编写一个社交网络应用程序,用户可以在其中关注其他用户及其活动。

在服务器端,每个用户都使用一个在 60 分钟后过期的令牌进行标识。

如果令牌已过期,并且用户想要调用方法- (void) followUserWithID:(NSNumber *)targetUserID,我希望此方法首先调用我的autologinMethod(以确保用户的令牌现在有效)然后重复- (void) followUserWithID:(NSNumber *)targetUserID

注意:我不想让“checkValidToken”请求启动额外的 HTTP 请求。

-(void)commandWithParams:(NSMutableDictionary*)params command:(NSString *)command onCompletion:(JSONResponseBlock)completionBlock
{

    NSString *_path = [NSString stringWithFormat:@"%@%@",self.baseURL, command];
    NSLog(@"path: %@", _path );

    NSMutableURLRequest *apiRequest =
    [self multipartFormRequestWithMethod:@"POST"
                                    path:_path
                              parameters:params
               constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                   //TODO: attach file if needed
               }];


    AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //success!
        NSLog(@"%@",responseObject);

        completionBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //failure :(
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"ERROR"]);
        // Unable to establish a connection to the server.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server error"
                                                        message:@"Please try again later"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }];

    [operation start];
}



- (void)followUserWithID:(NSNumber *)targetUserID
{
    NSNumber *ownID = [[NSUserDefaults standardUserDefaults] objectForKey:@"id"];
    NSMutableDictionary *HTTPPostDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                               ownID, @"target_user_id",
                                               targetUserID, @"user_id",nil];

    [[WebAPI sharedInstance] commandWithParams:HTTPPostDictionary command:@"follow_user" onCompletion:^(NSDictionary *json){
        NSLog(@"%@", json);
    }];
}

【问题讨论】:

    标签: iphone ios objective-c-blocks token afnetworking


    【解决方案1】:

    你需要

    1. 检查令牌在AFNetworking完成块中是否有效
    2. 如果令牌已过期,请更新它,然后重试操作

    根据您的服务器在这种情况下提供的 HTTP 状态代码,您的支票将位于 successfailure 块中。

    这是一个粗略的大纲:

    if (/* the token has expired */) {
        AFHTTPRequestOperation *operationToRetryAfterTokenRenewal = [operation copy];
    
        //TODO: set the completion blocks for operationToRetryAfterTokenRenewal.
    
        [myTokenRenewer autologinMethodWithCompletionBlock:^{
                         [operationToRetryAfterTokenRenewal start];
                     }];
    
    
    }
    

    两个音符:

    1. 注意 TODO。当您复制AFHTTPRequestOperation 对象时,完成块不会保留,因此您需要重新设置它们。 (有关详细信息,请参阅 AFURLConnectionOperation NSCopying Caveats。)
    2. 您确实应该使用[[myHTTPClient sharedClient] enqueueHTTPRequestOperation:operation] 而不是[operation start],尤其是在您要进行文件上传时。这允许系统控制一次运行多少个操作,并在网络可达性暂时暂停时延迟运行这些操作。

    【讨论】:

    • 谢谢,这意味着一个单独的服务器请求来检查我的令牌的有效性。我实际上发布了我当前的令牌,并根据它的有效性进入成功或失败块。但是,我想在失败时重做整个操作。我无法弄清楚如何以聪明的方式做到这一点。我现在不能只调用相同的操作(如递归调用)吗?
    • 您不需要单独的请求来检查有效性。如果失败块是你进入无效令牌的地方,那么将我写的示例代码放在失败块中。
    • 你可以重做整个操作,就像在我的代码中一样,复制AFHTTPRequestOperation对象,重置完成/失败块,然后重新开始。
    猜你喜欢
    • 2018-07-26
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2017-10-28
    • 1970-01-01
    • 2021-03-05
    • 2017-03-26
    相关资源
    最近更新 更多