3

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

在服务器端,每个用户都使用一个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);
    }];
}
4

1 回答 1

2

你需要

  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 警告。)
  2. 你真的应该使用[[myHTTPClient sharedClient] enqueueHTTPRequestOperation:operation]而不是[operation start],特别是如果你要进行文件上传。这允许系统控制一次运行多少操作,并在网络可达性暂时暂停时延迟运行它们。
于 2013-06-19T15:51:11.720 回答