我正在使用此代码循环访问一个数组以下载多个文件并写入磁盘。AF网络下载多个文件
-(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];
,但问题是它调用成功块中的每个文件完成后,(这是它应该),但我只需要一个最终的回调重新加载一些数据,并最终进度HUD。
任何指向正确方向的指针都会很棒。
嘿, 你在哪里调用这个函数?在AppDelegate中还是在特定的控制器中? 另外,如果在下载完成之前退出应用程序会发生什么? 谢谢 – manishKungwani