2

How do I create a queue on AFNetwork 2.0 and set a completion handler for when the added oppertations are finished?

currently I have this

ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];

[queue setDelegate:self];
[queue setQueueDidFinishSelector:@selector(refeshInterface)];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[queue addOperation:request];

ASIFormDataRequest *request2 = [ASIFormDataRequest requestWithURL:url2];
[queue addOperation:request2];

[queue go]

But I need to convert it to AFNetwork. All the solutions I have found so far seem to use AFHTTPClient which doesn't exist in AFNetwork 2.0.

I am new to AFNetwork, so some examples would be greatly appreciated.

Thanks!

user1560196
  • 178
  • 2
  • 7

1 Answers1

11

You need

+ (NSArray *)batchOfRequestOperations:(NSArray *)operations
                        progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
                      completionBlock:(void (^)(NSArray *operations))completionBlock

method of AFURLConnectionOperation.

Please look at the following sample

NSMutableArray *operations = [NSMutableArray array];

for (DGSocialImage *socialImage in socialImages) {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:socialImage.url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFImageResponseSerializer new];
    [operations addObject:operation];
}
NSArray *batchOperations = [AFURLConnectionOperation batchOfRequestOperations:operations
                                                                progressBlock:NULL
                                                              completionBlock:^(NSArray *operations) {
    NSError *error;
    for (AFHTTPRequestOperation *op in operations) {
        if (op.isCancelled){
            return ;
        }
        if (op.responseObject){
            // process your responce here
        }
        if (op.error){
            error = op.error;
        }
    }
}];
[[NSOperationQueue mainQueue] addOperations:batchOperations waitUntilFinished:NO];
Avt
  • 16,927
  • 4
  • 52
  • 72
  • Thanks. This is exactly what I was looking for. I somehow missed this when reading the docs. – user1560196 Mar 22 '14 at 22:35
  • How would I generate an operation with POST data? I simply want to give it a URL, and an NSDictionary with values. I found this using NSUrlRequest, but it is very long and doesnt seem like much of a convenience method. http://stackoverflow.com/questions/10300353/nsurlrequest-post-data-and-read-the-posted-page – user1560196 Mar 22 '14 at 23:16
  • AFNetworking provides convenient methods for "**tasks**". But there is impossible to set a completion block for batch of tasks. If you are using operations you have to set up NSURLRequest. – Avt Mar 22 '14 at 23:25
  • How can this be achieved with AFURLSessionManager? – Vamshi Krishna Jun 04 '16 at 11:10