Search code examples
iosasynchronousnsoperationnsoperationqueuesynchronous

Replace asynchronous requests with synchronous requests in background thread in iOS?


I tried AFHTTPRequestOperation objects combined with other NSOperation objects placed into a queue. But now I know that in AFHTTPRequestOperation only requests are performed in correct order (not response processing blocks).

I don't need the correct order of requests but I need to handle their responses in a correct order and send a "success" notification at the end. If one of the steps is failed then cancel the sequence. The only idea I have is the following:

NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

//synchronous request1
...
//handle request1 response
...
//synchronous request2
...
//handle request2 response
...
//send notification about success or failure
...
}];

It looks crazy but correct. Is this code correct? Could you advice anything better?


Solution

  • You're right. The basic thing is that you have to synchronize the responses based on completion. and it does not matter if you use sync or async way. scheme: request(sync/async) -> completion -> request 2 (sync/async) -> completion...etc..