小编Big*_*igB的帖子

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];
Run Code Online (Sandbox Code Playgroud)

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

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

cocoa-touch objective-c ios afnetworking

5
推荐指数
2
解决办法
6673
查看次数

标签 统计

afnetworking ×1

cocoa-touch ×1

ios ×1

objective-c ×1