Objective c AFN网络下载多个文件
我使用这段代码在一个数组中循环以下载多个文件并写入磁盘Objective c AFN网络下载多个文件,objective-c,ios,cocoa-touch,afnetworking,Objective C,Ios,Cocoa Touch,Afnetworking,我使用这段代码在一个数组中循环以下载多个文件并写入磁盘 -(void)download { //set url paths for (NSString *filename in syncArray) { NSString *urlpath = [NSString stringWithFormat:@"http://foo.bar/photos/%@", filename]; NSURLRequest *request = [NSURLRequest requestWithURL:
-(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];
但问题是它在每个文件完成后调用success块(它应该这样做),但我只需要最后一次回调来重新加载一些数据并结束一个进度HUD
任何指向正确方向的指针都是很好的。也许有一天这会对某人有所帮助,但我能够使用一种可能存在重大问题的解决方法,但对于我的简单用法来说没问题 我只是在处理完同步数组后删除了其中的每一行,然后运行我需要的代码
[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,在所有操作完成时调用。应该正是你要找的 嘿,这个函数叫什么?在AppDelegate中还是在特定控制器中?此外,如果在下载完成之前退出应用程序,会发生什么情况?谢谢,我同意,这是一个更好的解决方案。