Is it possible to complete locking from ASIHTTPRequest / AFNetworking in the background thread?

I run my ASIHTTPrequest synchronously in a separate thread, for example:

//inside this start method I create my ASIHTTPRequest and start it dispatch_async(dispatch_get_global_queue(0, 0), ^{ [process start]; }); 

But lock completion is still triggered in the main thread. Is it possible to save the execution of the completion block in the same thread where the request was launched?

The only thing I can decide is to determine the send queue and manually execute the final block in the same thread, thereby maintaining a link to the created queue. But this does not solve the problem directly, because you go to an instant little moment in the main thread before redirecting the rest of the code to the created send queue.

Does anyone have a better solution?

EDIT: The same is true for AFNetworking completion blocks ...

+4
source share
2 answers

Good to answer my question: The ASIHTTPRequest framework does not have the ability to run termination blocks in another thread.

Instead, you can use the AFNetwork structure. Here you have two properties for any type of AFOperation called successCallbackQueue and failureCallbackQueue . If you can add a predefined " dispatch_queue_t " to handle the execution of success and failure blocks.

Hope this helps others with the same problem!

UPDATE: Example

  dispatch_queue_t requestQueue = dispatch_queue_create("requestQueue", NULL); AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:yourRequest]; operation.successCallbackQueue = requestQueue; operation.failureCallbackQueue = requestQueue; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // add code for completion } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // add code for failure }]; [operation start]; 
+4
source

Try using a specific queue of your own creation. Then you can (as soon as it finishes, in its completion block), return to the global queue to update any display.

0
source

Source: https://habr.com/ru/post/1398605/


All Articles