I have an app that uses
AFNetworking
for sending requests and after it finished downloading, it calls block where I create data models from JSON. During model initialization, there is async image loading with the following codedispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){
NSData *urlData = [NSData dataWithContentsOfURL:downloadURL];
}
I know that
dataWithContentsOfURL:
is synchronous, and bydocumentation... this method can block the current thread for tens of seconds on a slow network...
But as it's executing asynchronously in other thread, it shouldn't block the main thread. After some investigating, I found that starting from
URLSession:task:didCompleteWithError:
method that is placed inside of AFURLSessionManager.m
has the following blocks hierarchy:URLSession:task:didCompleteWithError: //1
|
dispatch_async(url_session_manager_processing_queue(), ^{ //2
//url_session_manager_processing_queue() - this is the default queue
|
dispatch_group_async(url_session_manager_completion_group(), dispatch_get_main_queue(), ^{ //3
|
//Inside of my callback
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //4
|
NSData *urlData = [NSData dataWithContentsOfURL:downloadURL];
If I set
AFHTTPSessionManager
's completionQueue
property:_sharedClient.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
Then
dispatch_group_sync ... //3
using Default queue, and the main thread is not blocked. Is there any explanation why without setting completionQueue
property my main thread is blocked? (Stack trace showing semaphore_wait_trap
in main thread)