Suppose I am doing an async connection to a web service, which by definition since it is async runs in a separate thread from the main thread.
Now lets say I put this job or block of code in a serial dispatch queue. Since serial dispatch queues don’t process more than 1 job at a time, but I am sending a job that is already async, would it then consider the job to be done after the call to the async job is made? or would it actually wait for the async job to get done before processing the next job?.
What about a concurrent queue, would the concurrent generated thread, generate yet another thread to process the async operation?
EDIT: I realize my question is not really clear, so my question is:
If I am using the same serial dispatch queue, and i dispatch using dispatch_async a block of code that already performs an async operation for example a NSURLConnection – initWithRequest:delegate: that runs async, will the block be considered completed by the serial queue after the async call?, and will that async call generate yet another thread?. Or will the queue still wait for job 1 that is already async to finish before processing the second job?.
When you dispatch to a serial queue, each dispatched block is processed one after the other. So if your first block takes a long time to process, the second block will not be called until the long-running first block is finished.
If you’re enqueueing with
dispatch_asyncthe new block is simply put to the end of the queue, the functiondispatch_asyncimmediately returns and you can go on. But the block won’t be executed until the previous blocks have finished.However,
dispatch_syncwill wait until the block got its turn to execute and finish. So in your case,dispatch_syncwill block until the long-running first block has finished.If you dispatch to concurrent queue, then the second block will get to run in a new thread and thus the first block will not prevent the second from running.
You could also create two queues and dedicate them to different tasks, for example have one queue only for your web service stuff and another queue for different tasks. It depends on how these operations relate to each other and which may be run in parallel and which must run one after another.