如何在多请求中使用AFNetworking?

时间:2014-12-17 03:50:59

标签: afnetworking multitasking

我同时有一些请求,我想处理它,直到它们全部完成。 以下是要求:

[self getProfileData];
[self checkNewSystemMessage];
[self checkChatMessage];

getProfileData看起来像:

AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer.timeoutInterval = TIMEOUT_INTERVAL;
NSString * portNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"portNumber"];

[manager GET:URL_ADDRESS(portNumber,@"JsonEntryManager") parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"%@",responseObject);
    //Do something...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self performSelector:@selector(getProfileData) withObject:nil afterDelay:REQUEST_AGAIN_TIME];
}];

和其他人是一样的。 当他们全部完成时我想做点什么。我想的唯一方法是逐个请求它们。显然这是一个愚蠢的想法。那我怎么知道结束时间呢?

2 个答案:

答案 0 :(得分:0)

在开始任何请求初始化计数器之前:

int counter = 0;

并在每个请求的成功块中执行

counter++;
if(counter==3)
{
    //Do what you need to do when all the request is completed.
    //Make some method for this.
}

答案 1 :(得分:0)

我也试图弄清楚这一点,但是现在成功为我工作的是检查{strong}成功中AFHTTPRequestOperationManager的OperationQueue内部的操作次数 阻止

[manager GET:URL_ADDRESS(portNumber,@"JsonEntryManager") parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"%@",responseObject);

if ([weakSelf.manager.operationQueue operationCount] == 0) {
       //Do something here when all the operations are done.
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self performSelector:@selector(getProfileData) withObject:nil afterDelay:REQUEST_AGAIN_TIME];
}];