39

For searching, the error message is:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

I know this means some UIKit code is called from a background thread, and I know the solution is to wrap the code in

dispatch_async(dispatch_get_main_queue(), ^(void){ <code> });

My problem is locating where to do that, as none of the printed stack traces reference my app code. My evidence to prove this negative is searching (command-f) in the debug output window for the name of my app; I have 24 stack traces dumped out and my app name is not in any of them, except at the top with the error message. I can post one of them, but that doesn't seem very useful.

In the cases I am working on today, this is happening when transitioning view controllers, after viewWillDisappear() and before viewWillAppear(). I have found parts of my code to wrap dispatch_async() around, but those are all handled now. I have breakpoints and debug messages where objects relevant to the view controllers are allocated and deallocated, and they all trigger after the exception messages appear. This is happening both on the simulator and my iOS9 iPhone, both in debug and release modes.

How can I identify the background code which is apparently modifying the UI?

Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
robm
  • 1,303
  • 1
  • 16
  • 24

3 Answers3

56

This code PSPDFUIKitMainThreadGuard causes assertions on UIKit access outside the main thread

Steps to use:

  1. Add to project and compile this file without ARC
  2. Move PSPDFAssert definition to the first of the file
  3. Comment calling of PSPDFLogError as it is not defined
  4. import <UIKit/UIKit.h>

Your app will crash and stop with any attemption to modify any UI element from background thread

For swift use the following code: NBUIKitMainThreadGuard

Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
  • 1
    Thank you! That nailed it immediately. Also had to `#import ` – robm Sep 21 '15 at 06:27
  • 2
    what do you mean by "Move PSPDFAssert definition to the first of the file"? – ngb Oct 17 '15 at 14:16
  • That is because it gives you a compilation errors as it is defined after using it – Mahmoud Adam Oct 17 '15 at 14:20
  • @ngb There is a block in the code that says `#define PSPDFAssert(expression, ...) \ do { if(!(expression)) { \ NSLog(@"%@", [NSString stringWithFormat: @"Assertion failure: %s in %s on line %s:%d. %@", #expression, __PRETTY_FUNCTION__, __FILE__, __LINE__, [NSString stringWithFormat:@"" __VA_ARGS__]]); \ abort(); }} while(0)` Move this from it's current position to before / above `PSPDFReplaceMethodWithBlock(Class c, SEL origSEL, SEL newSEL, id block)` – Jacob R Oct 24 '15 at 12:22
  • Do I not need to import the file anywhere or make any kind of call? – Jeremy Hicks Oct 27 '15 at 14:06
  • @JeremyHicks no you don't, just add it to your project and follow the steps above to fix the compilation errors – Mahmoud Adam Oct 27 '15 at 14:28
  • 1
    But Still Its Showing That Warning Now What To Do @MahmoudAdam – Hardik Vyas Oct 29 '15 at 05:22
  • @hardikvyas what warning? – Mahmoud Adam Oct 29 '15 at 08:52
  • Hi. added the files and followed the steps above. The building the application succeeded. I did not get any compilation errors. When I run the app, it breaks at `void PSPDFAssertIfNotMainThread(void) `. I am using AFNetworking to make API calls. – GoGreen Dec 16 '15 at 11:43
  • @GoGreen you should trace back the Stack Trace to see what is the method that causes this problem – Mahmoud Adam Dec 16 '15 at 13:39
  • Ok. I figured it out later. Thanks a lot @MahmoudAdam – GoGreen Dec 16 '15 at 14:06
  • I thank you, my mother thanks you, my mother's mother thanks you! I was going nuts trying to track this down and you nailed it. – user938797 Dec 19 '15 at 02:30
  • Note that it seems to work for me with ARC enabled. Also, use the following instead of commenting out PSPDFLogError: `#define PSPDFLogError(expression, ...) NSLog(__VA_ARGS__)` – Erroneous Jan 21 '16 at 16:15
  • AWESOME routine that found in 5 minutes what two of us were looking for for hours. – Owen Hartnett Mar 30 '16 at 18:34
  • I agree with Owen. This is awesome stuff and really saved me from a nightmare!! – skantner Apr 01 '16 at 20:41
  • Yep, took no longer than 5 minutes and found the problem immediately. Thank you! – Ruiz Jul 27 '16 at 12:19
  • I'm in XCode 7.2.1 and iOS 9.3, and the suggested code doesn't do anything. I've even tried the variants suggested in the comments above. – Logicsaurus Rex Aug 15 '16 at 23:09
  • 4
    [Swift Solution](https://github.com/nrbrook/NBUIKitMainThreadGuard) thanks to nrbrook. – Schemetrical Nov 12 '16 at 07:54
1

Edit your project scheme, and under the Run action, in the Diagnostics tab, under Runtime API Checking, make sure Pause in issues is checked. When any background UI updates occur, the app will pause.

enter image description here

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • I have Main Thread Checker set, but it doesn't stop anywhere, but I can see that error message in the console. I can't find the source of the problem because all the updates seem to run from MainThread (keyword "seem to") – bojan Aug 30 '19 at 00:56
-2
NSOperationQueue.mainQueue().addOperationWithBlock(){
[YOUR CODE]
}
RodolfoNeto
  • 483
  • 5
  • 4