I don't know why my question above is being voted down. as @rmaddy mentioned, the Apple doc for async
and sync
are unless.
Anyways, I did experiment tested myself for question. So I am going to post my answer here.
- What is the difference between async block and sync block? Can someone explain what it means?
Answer: For the code in the async, the line just below the async block will be executed immediately after the dispatchQueue finishes its dispatch job, i.e there are no blocking / waiting for the completion of the async block.
On the other hand, for the sync block, the line just below the sync block will be blocked and waiting for the completion of the sync block. Hence, in such case, the sequence of the execution of the code are ensured to be run in one line by one line sequentially.
- e.g In the main thread, in the middle execution of the main thread. What will it happen if I call myqueue.async{...} or myqueue.sync{...}?
Answer: myqueue
is just a queue. So the elements(dispatched code blocks/jobs/tasks/actions named whatever you like) can be running the main thread or not in the main thread. If we call myqueue.async{...}
in the main thread, the dispatched code block will not run in the main thread and will dispatch to another thread for its execution. If we call myqueue.sync{...}
in the main thread, the code block will just like a normal code statement so the line just after it will be blocked until the code block is finished and the execution of the them are all happening in the main thread.
For example, since viewDidLoad
is always being run in the main thread, if we call myqueue.async{...}
inside viewDidLoad
, the {...}
block will be dispatched to another thread to be executed and the line just below it won't wait for its completion and the execution won't blocked. If we call myqueue.sync{...}
, the {...}
block will happen in the main thread. So the line just below the block will just wait for completion of the block.