Difference between DispatchQueue.main.async and DispatchQueue.main.sync

后端 未结 4 2267
孤街浪徒
孤街浪徒 2020-11-28 01:46

I have been using DispatchQueue.main.async for a long time to perform UI related operations.



Swift provides both DispatchQueue.main.async

4条回答
  •  情深已故
    2020-11-28 02:02

    sync or async methods have no effect on the queue on which they are called.

    sync will block the thread from which it is called and not the queue on which it is called. It is the property of DispatchQueue which decides whether the DispatchQueue will wait for the task execution (serial queue) or can run the next task before current task gets finished (concurrent queue).

    So even when DispatchQueue.main.async is an async call, a heavy duty operation added in it can freeze the UI as its operations are serially executed on the main thread. If this method is called from the background thread, control will return to that thread instantaneously even when UI seems to be frozen. This is because async call is made on DispatchQueue.main

提交回复
热议问题