Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect UI actions outside of main thread

Tags:

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?

like image 315
Resh32 Avatar asked Apr 17 '13 06:04

Resh32


People also ask

How can I update UI from other than main thread?

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 } });

What is main thread checker?

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.

What is the UI 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.

What is main thread in IOS Swift?

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.


2 Answers

Xcode 9 and later

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.

Xcode 8.3 and earlier

Alternative, Xcode-way solution based on steipete's gist – symbolic breakpoint:

Xcode Symbolic Breakpoint

You can easily add breakpoints for some other methods on any UIKit class, like -[UIView setNeedsDisplay] etc.

like image 195
Michi Avatar answered Sep 24 '22 01:09

Michi


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.

like image 39
Nef10 Avatar answered Sep 23 '22 01:09

Nef10