So the first question is how does dispatch_async determines which thread to use? just picks it randomly? I need to do some parsing and core data stuff, so I don’t want to block UI thread and use dispatch_async, but after that I send a NSURLRequest to fetch some more data and the callback is never invoked (probably because the thread is already dead).
So what’s a good way of making it? And I can not use
sendAsynchronousRequest:queue:completionHandler:
because the deployment OS is 4. for now I just send request inside
dispatch_async(dispatch_get_main_queue(), ^{
});
which is inside dispatch_async(myQueue) block which does all the parsing and saving to core data. But it does not seem right to me, I mean there should be a way to use dispatch_async and tell it not kill the tread, right? Because using sync requests is not an option.
You should not be thinking of threads when using GCD. That’s all handled behind the scenes for you. You think in queues which is what you pass to
dispatch_asyncalong with the block to execute. A GCD queue does sort of map to a thread, but you don’t need to worry about what is going on behind the scenes – GCD will handle all that for you.As for your code which I assume looks similar to this:
There is nothing wrong with that at all. You do not need to worry that the thread that the block you sent to
myQueueruns on, might be terminated. The queue will stay around until the block as finished running. The fact that you dispatch onto the main queue within the original dispatch is fine – the work on the main queue will happily run perfectly fine.I think you’re also asking why when using
NSURLRequestwithin an async dispatch you are seeing it not ever invoke the callback. That will be because it’s tightly coupled to the current run loop and if you’re currently on a background thread (which yourmyQueuewill be running on) then that run loop won’t be run again until another block is put on the queue. So your callback never runs.I encourage you to go and read more about GCD and what async vs sync actually means as I feel that you are possibly not fully understanding it yet. I answered a similar question about that here.