문제

I start my ASIHTTPrequest synchronously in a separate thread like this :

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

But the completionBlock is still fired on the main thread. Can one keep the execution of the completionBlock in the same thread as where the request was started?

The only thing i can come up with to solve this is to define a dispatch queue and manually execute the completionBlock in the same thread, thus holding a reference to a created queue. But that does not solve it directly because you would pass for a freaking little moment in the main thread before redirecting the rest of the code to the created dispatch queue.

Does someone have a better solution?

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

도움이 되었습니까?

해결책

Ok, to answer my own question : ASIHTTPRequest framework does not have an option to start completion blocks in a different thread.

Instead one can use the AFNetwork framework. Here you have two properties on any type of AFOperation called 'successCallbackQueue' and 'failureCallbackQueue'. Where you can add a predefined 'dispatch_queue_t' to handle the execution of success and failure blocks.

Hope this will help 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];

다른 팁

Try using a defined queue of your own creation. Then you can (once it finally finishes, in its completion block) signal back to the global queue to update any display necessary.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top