AFNetworking 2.0 uploads multiple images with completion

I am trying to figure out a way to upload multiple images using AFNewtorking 2.0. I 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 the downloads have ended, and if all the images have been downloaded. So I have an array with an ant image url 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 found several answers by queuing these requests and setting the maximum parallel operations to 1. But I do not know how this works.

Any help is appreciated, thanks in advance!

+4
ios objective-c afnetworking afnetworking-2
source share
3 answers
 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]; 
+3
source share

Try the following:

 // _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, ^{ } 
+2
source share

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

Although not documented, the implementation is self-explanatory. It also allows you to track progress.

Before using this method, you will need an array of HTTP operations (this is if you decide to stick with the NSNLCLConnection AFNetworking implementation).

+1
source share

All Articles