How to Batch Request with Afnetworking 2

How to batch request with AFNetworking 2?

Thanks Sendoa for the link to the GitHub issue where Mattt explains why this functionality is not working anymore. There is a clear reason why this isn't possible with the new NSURLSession structure; Tasks just aren't operations, so the old way of using dependencies or batches of operations won't work.

I've created this solution using a dispatch_group that makes it possible to batch requests using NSURLSession, here is the (pseudo-)code:

// Create a dispatch group
dispatch_group_t group = dispatch_group_create();

for (int i = 0; i < 10; i++) {
// Enter the group for each request we create
dispatch_group_enter(group);

// Fire the request
[self GET:@"endpoint.json"
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
// Leave the group as soon as the request succeeded
dispatch_group_leave(group);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
// Leave the group as soon as the request failed
dispatch_group_leave(group);
}];
}

// Here we wait for all the requests to finish
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// Do whatever you need to do when all requests are finished
});

I want to look write something that makes this easier to do and discuss with Matt if this is something (when implemented nicely) that could be merged into AFNetworking. In my opinion it would be great to do something like this with the library itself. But I have to check when I have some spare time for that.

Uploading an array of images using AFNetworking 2 Batch requests

Is imageArray definitely an array of URL's? Somewhere, someone's expecting a string, but they're getting an image. Based on:lastPathComponent (a NSString method) being called on a UIImage.

AFNetworking 2 composing batch with dependencies

You shouldn't use NSOperation dependencies for this because later operations rely on processing with completionBlock but NSOperationQueue considers that work a side effect.

According to the docs, completionBlock is "the block to execute after the operation’s main task is completed". In the case of AFHTTPRequestOperation, "the operation’s main task" is "making an HTTP request". The "main task" doesn't include parsing JSON, persisting data, checking HTTP status codes, etc. - that's all handled in completionBlock.

So in your code, if the ssoA operation succeeds in making a network request, but authentication fails, all the later operations will still continue.

Instead, you should just add dependent operations from the completion blocks of the earlier operations.

When adding an op from the completion block of another, can I add it to the "batch" (for overall batch completion tracking)?

You can't, because:

  1. At this point it's too late to construct a batch operation (see the implementation)
  2. It doesn't make sense, because the later operations may not ever get created (for example, if authentication fails)

As an alternative, you could create one NSProgress object, and update it as work progresses to reflect what's been done and what is known to remain. You could use this, for example, to update a UIProgressView.

Is there a better way (other than persisting data in each op and then reading from storage in the successor op) to pass data between dependent operations in a batch?

If you add dependent operations from the completion blocks of the earlier operations, then you can just pass local variables around after validating the success conditions.

Error:Request failed: bad request (400) afnetworking batch in swift

You should better use this Swift client for the Batch.com Transactional API.

All HTTP response error codes for this API are explained in the documentation but a 400 Bad request error shouldn't happening, it must be a temporary error on Batch.com's end.

I suppose you have replaced REST API KEY, USER_TOKEN and API KEY with valid values?



Related Topics



Leave a reply



Submit