How does Dispatch.main.async “update the UI”?

后端 未结 2 750
说谎
说谎 2021-01-27 06:38

I\'ve been using Swift for a little while and GCD still confuses me a bit.

I\'ve read:

https://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1

相关标签:
2条回答
  • 2021-01-27 07:16

    Dispatch.main.async does not update the UI. The story goes into a different direction: If you want to update the UI, you must do so from the main thread. If you're current code is not running on the main thread, Dispatch.main.async is the most convenient way to have some code run on the main thread.

    It's an old restrictions that affects most operating systems: UI related actions such as changing elements in the UI must only be called from a specific thread, usually the so called main thread.

    In many cases that's not a problem since your UI related code usually acts when triggered by some UI event (user clicking or tapping, key pressed etc.). These event callback happen on the main thread. So there is no threading issue.

    With GCD, you can run long-running tasks on separate threads so the tasks doesn't slow down or even block the UI. So when these tasks are finished and you want to update the UI (e.g. to display the result), you must do so on the main thread. With Dispatch.main.async you can ask GCD to run a piece of code on the main thread. GCD doesn't know about the UI. Your code must know what to update. GCD just runs your code on the desired thread.

    If at the end of your tasks there is nothing to display or otherwise update in the UI, then you don't need to call Dispatch.main.async.

    Update re Firebase

    The Firebase Database client performs all network and disk operations in separate background thread off the main thread.

    The Firebase Database client invokes all callbacks to your code on the main thread.

    So no need to call Dispatch.main.async in the Firebase callbacks. You are already on the main thread.

    0 讨论(0)
  • 2021-01-27 07:27

    FYI the reason that all of the UI code needs to go on the main thread is because drawing is a (relatively in CPU time) long and expensive process involving many data structures and millions of pixels. The graphics code essentially needs to lock a copy of all of the UI resources when its doing a frame update, so you cannot edit these in the middle of a draw, otherwise you would have wierd artifacts if you went and changed things half way through when the system is rendering those objects. Since all the drawing code is on the main thread, this lets he system block main until its done rendering, so none of your changes get processed until the current frame is done. Also since some of the drawing is cached (basically rendered to texture until you call something like setNeedsDisplay or setNeedsLayout) if you try to update something from a background thread its entirely possible that it just won't show up and will lead to inconsistent state, which is why you aren't supposed to call any UI code on the background threads.

    0 讨论(0)
提交回复
热议问题