AFnetworking下载多个文件 [英] AFnetworking downloading multiple files

查看:150
本文介绍了AFnetworking下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this code to loop through an array to download multiple files and write to disk.

-(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。

but the problem is it calls the success block after each file is done, (which it should) but I just need one final call back to reload some data and end a progress HUD.

任何指向正确方向的指针将是巨大的。

Any pointers in the right direction would be great.

推荐答案

也许有一天,这将帮助某人,但我能够使用一个解决方法,可能有重大问题,但它可以为我的简单用法。

Maybe someday this will help someone, but I was able to use a workaround that probably has major issues but its okay for my simple usage.

我刚刚删除了同步数组中的每一行,在它被处理后,然后运行我需要的代码。

I just deleted each line from the sync array after it was processed then ran my code i needed.

 [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"];
    }

这篇关于AFnetworking下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆