Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does AFHTTPRequestOperationManager run requests on the operationQueue?

If I have a manager created and instance-varred and I do:

AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request success:mySuccessBlock failure:myFailureBlock];

[operation start];

will this run on the manager's operationQueue? I can't seem to find anything guaranteeing it will verse using one of the GET, POST, PUT methods instead which I assume will add the operation to the queue.

I see mattt's answer here, but I want to be sure one way or the other.

How send request with AFNetworking 2 in strict sequential order?

What I'm trying to do is queue up requests and have them run synchronously by setting

[self.manager.operationQueue setMaxConcurrentOperationCount:1]; 
like image 781
Mark W Avatar asked Feb 14 '23 17:02

Mark W


1 Answers

If you want to run an operation on a queue, you have to explicitly add the operation to the queue:

AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request success:mySuccessBlock failure:myFailureBlock];

[queue addOperation:operation];

This presumes that you would create a queue:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.name = @"com.domain.app.networkQueue";
queue.maxConcurrentOperationCount = 1;

If you just start the operations without adding them to a queue (e.g. [operation start]), they'll just run, without honoring any particular queue parameters.

Note, you could also have added your operation to the operation manager's queue (which you can set the maxConcurrentOperationCount, as you alluded to in your question):

[self.manager.operationQueue addOperation:operation];

That saves you from having to create your own queue. But it then presumes that all of your other AFNetworking requests for this manager would also run serially, too. Personally, I'd leave manager.operationQueue alone, and create a dedicated queue for my logic that required serial operations.

One final note: Using serial network operations imposes a significant performance penalty over concurrent requests. I'll assume from your question that you absolutely need to do it serially, but in general I now design my apps to use concurrent requests wherever possible, as it's a much better UX. If I need a particular operation to be serial (e.g. the login), then I do that with operation dependencies or completion blocks, but the rest of the app is running concurrent network requests wherever possible.

like image 175
Rob Avatar answered May 04 '23 01:05

Rob