AFNetworking + download images in queue + cancel operations

afnetworkingqueue

I need to download a queue of images.
I created my operations first, then add them with the "enqueue" method of AFNetworking.

I have 2 problems :
1) I didn't have the progress bar working for the queue (and I have it working with a custom operation queue)
2) I didn't find the solution to stop the queue when I want

I created first operations to batch and add theme in a array:

  while ((dict = [enumerator nextObject]))
  {    
    NSMutableURLRequest *request = [[MyHTTPClient sharedClient] requestWithMethod:@"GET" path:@"ws/webapp/services/pull_image" parameters:dict];

    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil                                                                                       cacheName:nil
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
    {
      NSLog(@"image : %@", [image description]);
      // process images
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
    {
      // manage errors
    }];

    [operations addObject:operation];
  }

Then, I enqueue the operations:

     [[MyHTTPClient sharedClient] enqueueBatchOfHTTPRequestOperations:operations 
       progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) 
       {
           float percentDone = ((float)((int)numberOfCompletedOperations) / (float)((int)totalNumberOfOperations));
  [delegate syncServicesController:self updateProgressView:percentDone];
       } 
       completionBlock:^(NSArray *operations) 
      {
        //
      }];

So, the progress download didn't work.
But I can see the progress of numberOfCompletedOperations… ? 1,2,3,4,5… Does I need to force the refresh of the progress view in the main thread ?

And when I tried to stop the network tasks:

- (void)cancelAllRequests
{
  [[MyHTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"ws/webapp/services/pull_image"];

}

I don't understand how to stop the queue of requests… This seems that works but I have this error : -[NSBlockOperation request]: unrecognized selector sent to instance 0x16f54c70

Best Answer

These were actually just fixed in the last day or two :)

Go ahead and update to the latest version of master, which includes the following:

cc2115e469: Progress blocks now dispatch to main by default, just like all of the other completion blocks in AFNetworking. This should fix any issues around the UI not updating there.

cac44aeb34: Fixes that problem with NSBlockOperation being sent request. There was an incorrect assumption baked into cancelAllHTTPOperationsWithMethod: that all operations were AFHTTPRequestOperation. The only downside is that it will not handle your batched operations. For that, you can always iterate through httpClient.operationQueue.operations and pick out the one you want.

Related Topic