22

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?

Community
  • 1
  • 1
Resh32
  • 6,500
  • 3
  • 32
  • 40
  • I found this interesting and investigating it: http://stackoverflow.com/questions/10424979/how-do-i-set-a-symbolic-breakpoint-on-webthreadlockfromanythread-in-xcode – Resh32 Apr 17 '13 at 09:13
  • This code (just add to project and compile this file without ARC) causes assertions on UIKit access outside the main thread: https://gist.github.com/steipete/5664345 I've just used it to pickup numerous UIKit/main thread issues in some code I've just inherited. – Andrew Ebling Sep 12 '13 at 13:55

3 Answers3

43

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.

Michi
  • 1,464
  • 12
  • 15
4

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.

Stickley
  • 4,561
  • 3
  • 30
  • 29
Nef10
  • 926
  • 2
  • 16
  • 26
1

Xcode 11/Swift version of @Michi 's answer enter image description here

WINSergey
  • 1,977
  • 27
  • 39