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

后端 未结 2 751
说谎
说谎 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题