0

我正在尝试制作一个使用AFHTTPClient. 第一步是对用户进行身份验证,为此,我使用以下代码:

-(void)authorize
{
    NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"login.php" parameters:@{@"login": account.username, @"passwd":account.password}];

    AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [httpClient setDefaultHeader:@"Token" value:[[operation.response allHeaderFields] objectForKey:@"CSRF-Token"]];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    [httpClient enqueueHTTPRequestOperation:operation];
    [httpClient.operationQueue waitUntilAllOperationsAreFinished];
}

如您所见,我的代码从服务器响应中获取了一个令牌,并将其设置为所有未来请求的默认标头。

然后我继续使用其他请求- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:success failure:failure

但是当我使用调试器时,我发现那些在授权操作完成之前执行的其他请求,因此它们失败了,因为它们没有授权令牌。我添加了[httpClient.operationQueue waitUntilAllOperationsAreFinished];,但它似乎不起作用......

感谢您的帮助

4

1 回答 1

2

使用调度信号量

-(void)authorize
{
    dispatch_semaphore_t done = dispatch_semaphore_create(0);
    NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"login.php" parameters:@{@"login": account.username, @"passwd":account.password}];

    AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [httpClient setDefaultHeader:@"Token" value:[[operation.response allHeaderFields] objectForKey:@"CSRF-Token"]];
        dispatch_semaphore_signal(done);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    [httpClient enqueueHTTPRequestOperation:operation];
    [httpClient.operationQueue waitUntilAllOperationsAreFinished];
    dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
}

请注意,这将阻止该方法返回,因此您需要确保它不在主/UI 线程上。

于 2013-02-17T20:34:36.940 回答