Note: This question is related to Warn on calls to UIKit from background threads but does not give an answer on two of the approaches below.
I have a problem where the app screen blinks rapidly. I already had that problem in the past and it is due to updating the UI elements outside the main thread.
Therefore I've put the following code in many places:
assertMainThread();
which is:
#define assertMainThread() NSAssert([NSThread isMainThread],@"Method called using a thread other than main!")
Of course I cannot cover the whole code with assertMainThread() as there are many places and some code is used in a legitimate way by background GCD queues.
I looked at many places, but could not find a way for XCode or LLDB to tell me when a UI element is updated outside the main thread. I thought that one could use symbolic breakpoints or some other mechanism to break at the place where a common method in UIKit is called outside of main thread for example, but could not find a way.
I also thought that UIKit could warn at runtime when such a call is made? Or at least give us some tools to see help debug such problems.
Another approach I looked (but did not try) is to use some code coverage techniques and try to extract what thread was running at what point in the code visually, but did not go that route.
Do you have any idea on how to tackle the problem?
In this case, to update the UI from a background thread, you can create a handler attached to the UI thread, and then post an action as a Runnable : Handler handler = new Handler(Looper. getMainLooper()); handler. post(new Runnable() { @Override public void run() { // update the ui from here } });
The Main Thread Checker (MTC) is a runtime tool that throws a warning when system API calls that should be made on the main thread, such as UI operations, are incorrectly called on a background thread.
User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.
The main thread is the one that starts our program, and it's also the one where all our UI work must happen. However, there is also a main queue, and although sometimes we use the terms “main thread” and “main queue” interchangeably, they aren't quite the same thing.
Xcode 9 introduces a Main thread checker which sniffs for many relevant issues potentially performed out-of-main thread. You can enable this analyser the usual way in your Scheme options along with other runtime analysers.
Alternative, Xcode-way solution based on steipete's gist – symbolic breakpoint:
You can easily add breakpoints for some other methods on any UIKit
class, like -[UIView setNeedsDisplay]
etc.
Starting with Xcode 9 Apple integrated an Main Thread Checker in Xcode which is enabled by default if you run you app in with the Xcode debugger.
It can be disabled/enabled in the scheme configuration under the diagnostics tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With