AFnetworking下载多个文件

我正在使用此代码循环访问数组,以下载多个文件并写入磁盘。

-(void)download { //set url paths for (NSString *filename in syncArray) { NSString *urlpath = [NSString stringWithFormat:@"http://foo.bar/photos/%@", filename]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:filename]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Successfully downloaded file to %@", path); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [operation start]; 

但问题是每个文件完成后(它应该)调用成功块,但我只需要最后一个callback重新加载一些数据,并结束进展HUD。

任何指向正确方向的指针都会很棒。

也许有一天这将有助于某人,但我能够使用一个解决scheme,可能有重大问题,但它对我的简单用法是好的。

我刚从同步数组中删除每行后处理,然后运行我需要的代码。

  [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Successfully downloaded file to %@", path); [SVProgressHUD showWithStatus:@"Updating Photos"]; [syncArray removeObject:filename]; if (!syncArray || !syncArray.count) { NSLog(@"array empty"); [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self]; [SVProgressHUD dismissWithSuccess:@"Photos Updated"]; } 

您可以使用AFHTTPClient来排队批处理,并有一个completionBlock,当所有操作完成时调用。 应该是你正在寻找的。