Group Group Group Group Group Group Group Group Group

iOS Concurrency with GCD and Operations - Part 2: | Ray Wenderlich

Is running a task synchronously on the main queue the same as running directly in the main thread?
In the first case, running a task synchronously on the main queue means the current queue (main queue or the main thread) has to wait till the task has been completed.
In the second case, the main thread executes the task and subsequent tasks thereafter.
Please note that in first case, because of the synchronicity, the main thread has to stop anyway so instead of waiting for the task to complete, it decides to run the task instead?

Question 2:
Main queue is a serial queue, hence often called main thread.
When I run a task asynchronously on the main queue, the new async task I ran runs on a different queue, which is no longer a main queue. So whats the difference between DispatchQueue.main.async{ } and ), since both queues will be off the main queue

well, you shouldn’t run synchronously on the main queue but if you do, then the dispatcher will probably reuse the main thread because it isn’t doing anything else while it’s waiting. The same is true if you run synchronously on any other serial queue: its thread will be available to do the task.

you run DispatchQueue.main.async from a non-main queue where you’ve been doing slow non-UI work, to update the UI: any UI update tasks must run on the main queue.

you run from any queue that isn’t the default global queue, including from the main queue, to move work onto that queue.

Wow this was deep, needed to watch it 3-4 times to understand every word spoken…

Just a query, suppose I have a private serial queue, and it has 2 slow tasks lined up asynchronously. What happens if I add a synchronous task to that queue, would the synchronous task be executed by the main thread after the 2 slow tasks in private serial queue are executed?

If so, that’s undesirable (running non UI tasks in main thread), so the sync task should already be running on a non-main queue?

again, you shouldn’t dispatch synchronously from the main queue

dispatching any task, from any queue, onto a private serial queue: that task must wait its turn while the serial queue runs the earlier tasks

  • sync/async dispatch blocks/doesn’t-block the source queue
  • serial/concurrent describes how the destination queue runs tasks when there are more than one
1 Like

@akashlal Please check out the updated version of the course when you get a chance:

I hope it helps!

1 Like