Note that there are some explanatory texts on larger screens.

plurals
  1. POWarn on calls to UIKit from background threads
    primarykey
    data
    text
    <p>iOS's UIKit is not thread-safe, let us call this fact well known. I know the rule, I'm careful, but I still get bitten - and every now and then the resulting crash is far enough removed from the offending background call into UIKit to make tracking down the problem a less than joyeus experience. </p> <p>This problem seems like it could be easy to solve - have UIKit classes/methods warn when they are invoked from a background thread, at least as a debug feature. As far as I'm aware, iOS does not provide any such feature. Of course one could achieve the same effect manually by having some form of assertions precede such calls, but this solution is not the most elegant and in addition suffers from the same weakness as the original problem, namely that programmers are prone to forgetfulness. </p> <p>Does anyone have a more elegant solution? How do you deal with this problem in your projects?</p> <p>(Note: <a href="https://stackoverflow.com/questions/7839229/how-can-i-find-calls-to-uikit-instances-from-a-secondary-thread" title="this">this question</a> is related, but not quite as explicit. One is left wondering)</p> <hr> <p><strong>UPDATE</strong>: Andrew's answer is the solution I was looking for at the time, however note that at least as of Xcode 9 this is now provided by xcode/ios. For instance, adding this code:</p> <pre><code>DispatchQueue.global().async { print(self.view.frame) } </code></pre> <p>To a UIView's viewDidLoad method produces a runtime warning inline in Xcode <em>UIView.frame must be used from the main thread only</em> and a message printed to the console: <em>Main Thread Checker: UI API called on a background thread: -[UIView frame]</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload