KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am executing a block using dispatch_sync and the block is executed correctly. But this block is executed on the main thread. As per the Apple Doc: Serial queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue. The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue. which means (or what I understood) that current process that is being executed will run on a separate thread. Below is the code that I am using to judge what's going on. It is being called inside NSURLConnection's didReceiveData: delegate method (I know I should not do that inside the didReceiveData: delegate method - but this is just a sample to focus on dispatch_sync). Following are the different ways that I can assume as a proof of my conclusion: Using dispatch_sync on a Global Concurrent Queue dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if ([NSThread isMainThread]) { NSLog(@"Main Thread"); } else NSLog(@"Not on Main Thread"); //Some Process }); Output - Main Thread Main Thread Main Thread // Main Thread printed till didReceiveData: gets called Using dispatch_sync on a self created queue using dispatch_queue_create // Create queue somewhere else like this dispatch_queue_t newQueue = dispatch_queue_create("WriteQueue", DISPATCH_QUEUE_SERIAL); dispatch_sync(newQueue, ^{ if ([NSThread isMainThread]) { NSLog(@"Main Thread"); } else NSLog(@"Not on Main Thread"); //Some Process }); </
Tags (comma-separated)
Save Edits
Cancel