UIKit and GCD thread-safety

前端 未结 2 1531
借酒劲吻你
借酒劲吻你 2021-01-15 01:23

Many of the posts say that UIKit is totally not thread safe. Now on Apple documentation for GCD we can read that it\'s the DRAWING that is not thread safe. So would code lik

相关标签:
2条回答
  • 2021-01-15 02:15

    My personal opinion is that what you have heard so far is misleading.

    Here is a quote from Apple's UIKit Framework Reference, credit to a similar thread here:

    Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

    Also, note, that starting from iOS4, contrary to what you have assumed, according to release notes found here,

    Drawing to a graphics context in UIKit is now thread-safe. Specifically:

    • The routines used to access and manipulate the graphics context can now correctly handle contexts residing on different threads.
    • String and image drawing is now thread-safe.
    • Using color and font objects in multiple threads is now safe to do.

    In fact furthermore in WWDC 2012 session "Building Concurrent User Interfaces", Apple engineer demonstrates drawing into image graphics context in a background thread.

    My take-away:

    • You can use multi-threaded drawing, but don't assume anything. Use only what is explicitly declared by Apple.
    • Again, assume nothing. Don't touch any of the UIKit stuff that inherits from UIResponder from background thread.
    0 讨论(0)
  • 2021-01-15 02:20

    In this example, you're not changing any values so yes,it's ok. Updating UI from background thread is not.

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