AFNetworking 2.0 download multiple images with completion

本小妞迷上赌 提交于 2020-01-10 15:38:06

问题


I'm trying to figure out a way to download multiple images with AFNewtorking 2.0. I've read a lot of posts here in SO, but can't find the answer I'm looking for, hope you guys can help me.

The problem is that I want to know when all of the downloads finished and if all images where downloaded. So I have an array with image URL's ant trying to do something like this.

for(NSString *photoUrlString in self.photos){

        NSURL *url = [NSURL URLWithString:photoUrlString];
        AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
        requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
        [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Image error: %@", error);
        }];
        [requestOperation start];
    }

I've found some answers with putting these requests into a queue and setting max concurrent operations to 1. But don't know how that works really.

Any help is appreciated, thanks in advance!


回答1:


for(Photo *photo in array){

    //form the path where you want to save your downloaded image to
    NSString *constPath = [photo imageFullPath];

    //url of your photo
    NSURL *url = [NSURL URLWithString:photo.serverPath];

    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
    op.responseSerializer = [AFImageResponseSerializer serializer];

    op.outputStream = [NSOutputStream outputStreamToFileAtPath:constPath append:NO];
    op.queuePriority = NSOperationQueuePriorityLow;
    [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){

    }];

    op.completionBlock = ^{

        //do whatever you want with the downloaded photo, it is stored in the path you create in constPath
    };
    [requestArray addObject:op];
}

NSArray *batches = [AFURLConnectionOperation batchOfRequestOperations:requestArray progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
} completionBlock:^(NSArray *operations) {

    //after all operations are completed this block is called
    if (successBlock)
        successBlock();
}];

[[NSOperationQueue mainQueue] addOperations:batches waitUntilFinished:NO];



回答2:


Try this:

// _group, _queue are iVar variable
dispatch_group_t *_group = dispatch_group_create();
dispatch_queue_t *_queue = dispatch_queue_create("com.company.myqueue2", NULL);

// all files download
for(int i = 0 ; i < numberOfFileDownloads; i++){
   dispatch_group_async(_group, _queue, ^{
      // here is background thread;
      // download file
   });
}


// all files are download successfully, this method is called
dispatch_group_notify(_group, _queue, ^{
}



回答3:


Check out +[AFURLConnectionOperation batchOfRequestOperations:progressBlock:completionBlock:]

Although it's not documented, implementation is self-explanatory. Also it allows you to monitor the progress.

You will need to have an array of HTTP operations prior to using this method (this is if you decided to stick to NSURLConnection-based implementation of AFNetworking).



来源:https://stackoverflow.com/questions/23671720/afnetworking-2-0-download-multiple-images-with-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!