AFNetworking:下载队列中的文件 [英] AFNetworking: downloading files in queue

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

问题描述

我刚刚开始使用AFNetworking,到目前为止,它仅能从Web上获取数据就可以正常工作。

I just started using AFNetworking and so far it worked fine for just getting data from the web.

但是现在我有了一个需要下载的文件列表

But now I have a list of files that need to be downloaded to the device

for(NSDictionary *issueDic in data)
{
    Issue *issue = [[Issue alloc] init];
    ...
    [self getOrDownloadCover:issue];
    ...
}

getOrDownloadCover:issue检查文件是否已存在在本地,如果有,它只会保存该路径,如果没有,它将从给定的URL下载文件

getOrDownloadCover:issue checks if a file already exists locally, if it does, it just saves that path, if it doesn't, it downloads the file from a given url

- (void)getOrDownloadCover:(Issue *)issue
{
    NSLog(@"issue.thumb: %@", issue.thumb);

    NSString *coverName = [issue.thumb lastPathComponent];

    NSLog(@"coverName: %@", coverName);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    __block NSString *filePath = [documentsDirectory stringByAppendingPathComponent:coverName];

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        // Cover already exists
        issue.thumb_location = filePath;

        NSLog(@"issue.thumb_location: %@", filePath);
    }
    else 
    {
        NSLog(@"load thumb");

        // Download the cover
        NSURL *url = [NSURL URLWithString:issue.thumb];
        AFHTTPClient *httpClient = [[[AFHTTPClient alloc] initWithBaseURL:url] autorelease];
        NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:issue.thumb parameters:nil];

        AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        issue.thumb_location = filePath;

        NSLog(@"issue.thumb_location: %@", filePath);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"FAILED");
    }];
        [operation start];
    }
}

getOrDownloadCover:issue可以连续调用20次,因此我需要将所有请求放入一个队列,一旦队列完成,它仍然应该能够保存路径(或者只是发出通知,因为我已经知道路径是什么了)

getOrDownloadCover:issue can be called 20 times in a row, so I would need to put all my requests into a queue, and once the queue is completed, it should still be able to save the path (or just send out a notification, since I already know what the path is)

对此有任何建议吗?

推荐答案

添加 NSOperationQueue AFHTTPRequestOperation 添加到此队列中,例如:

Add an NSOperationQueue to an object you can get the instance of at any point in your application, for example in your appDelegate. Then just add the AFHTTPRequestOperation to this queue like:

[[(AppDelegate *) [UIApplication sharedApplication].delegate operartionQueue] addOperation:operation];

只需处理完成块中的保存,即可在主线程上从该块中调用方法或发布 NSNotification

Just handle the saving in the completion block, either call a methode from this block on the main thread or post a NSNotification.

使用GCD调用主线程:

To call the main thread use GCD :

dispatch_async(dispatch_get_main_queue(), ^{
    // Call any method from on the instance that created the operation here.
    [self updateGUI]; // example
});

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

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