I use AFNetworking as my connection lib to my app. Due to the back-end restrictions, I cannot send two request simultaneously when the app starts because the server will identify a CookieTheftException (Grails). After a first successful connection, I can do as many simultaneous requests as I want but the first need to be serial.
How can I achieve that?
I thought using a Semaphore but i can’t block the main thread.
Edit 1
I tried to override but it didn’t work. I think the operation queue doesn’t wait one request to finish (including it’s callback) to start the other.
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation
{
[self.operationQueue setMaxConcurrentOperationCount:1];
[super enqueueHTTPRequestOperation:operation];
}
Edit 2
I realized that the maxConcurrentOperation worked and in fact 1 operation is executed at a time. The problem is that the request enqueued is already created without the cookies the server needs.
I don’t know anything about Grails or the specific architecture of your system, but perhaps this could be solved by simply turning off cookies on that request, with
NSMutableURLRequest -setHTTPShouldHandleCookies:.Other than that, the best way to ensure that only one request operation is ever running for that initial call would be to ignore queues altogether, and simply have an
AFHTTPRequestOperationproperty on yourAFHTTPClientsubclass. You could even get fancy with KVO to ensure that the operation queue is suspended until that initial request is finished.