This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…

PSPDFAssert(NSThread.isMainThread, @"\nERROR: All calls to UIKit need to happen on the main thread. You have a bug in your code. Use dispatch_async(dispatch_get_main_queue(), ^{ ... }); if you're unsure what thread you're in.\n\nBreak on PSPDFAssertIfNotMainThread to find out where.\n\nStacktrace: %@", NSThread.callStackSymbols);

This comment has been minimized.

I'm considering enabling this in non-debug builds, changing the assert to a Crashlytics log call. The idea is to have a clue in the crash report that this is what (likely) caused the crash. How bad an idea is that?

I'm considering enabling this in non-debug builds, changing the assert to a Crashlytics log call. The idea is to have a clue in the crash report that this is what (likely) caused the crash. How bad an idea is that?

This comment has been minimized.

@jklundell It doesn't use any private API and the performance impact is minimal - so you should be fine. Just don't hard-crash here, we don't know if Apple always plays by the rules (e.g. they use CALayer in a thread with UIWebView...)

@jklundell It doesn't use any private API and the performance impact is minimal - so you should be fine. Just don't hard-crash here, we don't know if Apple always plays by the rules (e.g. they use CALayer in a thread with UIWebView...)

This comment has been minimized.

@segiddins It probably will work as well with ARC, but the compiler might put retains/releases in there where we don't want them, e.g. when calls happen during dealloc where we would then create a (short) resurrection of the object.

Update: I've added __unsafe_unretained, that should do the trick as well.

@segiddins It probably will work as well with ARC, but the compiler might put retains/releases in there where we don't want them, e.g. when calls happen during dealloc where we would then create a (short) resurrection of the object.

Update: I've added __unsafe_unretained, that should do the trick as well.

This comment has been minimized.

Same as michaeleisel. Have the following output in the console: 2013-08-02 09:26:31:640 [846:3079] Failed to add method: pspdf_setNeedsLayout on UIView
[846:3079] Failed to add method: pspdf_setNeedsDisplay on UIView
[846:3079] Failed to add method: pspdf_setNeedsDisplayInRect: on UIView

Same as michaeleisel. Have the following output in the console: 2013-08-02 09:26:31:640 [846:3079] Failed to add method: pspdf_setNeedsLayout on UIView
[846:3079] Failed to add method: pspdf_setNeedsDisplay on UIView
[846:3079] Failed to add method: pspdf_setNeedsDisplayInRect: on UIView

This comment has been minimized.

I have DEBUG=1 preprocessor macro enabled for only debug builds (and have confirmed #if DEBUG ...#endif works correctly elsewhere in my code). However this assertion code still seems to be present in Release builds - I've written a little test app which deliberately hits UIKit from a background thread to test it.

I have DEBUG=1 preprocessor macro enabled for only debug builds (and have confirmed #if DEBUG ...#endif works correctly elsewhere in my code). However this assertion code still seems to be present in Release builds - I've written a little test app which deliberately hits UIKit from a background thread to test it.

This comment has been minimized.

@vandadschibsted I was having the same issue and came up with a simple fix. I removed the if (_self.window) checks from the swizzled methods, that did the trick for me... thanks to this I found the cause of an issue that was disabling all animations across the app!

@vandadschibsted I was having the same issue and came up with a simple fix. I removed the if (_self.window) checks from the swizzled methods, that did the trick for me... thanks to this I found the cause of an issue that was disabling all animations across the app!

I am getting the following Assertion failure.
Assertion failure: NSThread.isMainThread in void PSPDFAssertIfNotMainThread() on line /Users/<account_name>/Documents/Tutorials/TestSample/PSPDFUIKitMainThreadGuard.m:146.

I am getting the following Assertion failure.
Assertion failure: NSThread.isMainThread in void PSPDFAssertIfNotMainThread() on line /Users/<account_name>/Documents/Tutorials/TestSample/PSPDFUIKitMainThreadGuard.m:146.

This comment has been minimized.

This comment has been minimized.

Still not clear to me how to actually use this. The file is in my project and it appears in the Compile Build Phase. Do I need to import the file in the AppDelegate or somewhere and is there a call I need to make? I ask because it's not catching any problems but the new version of Xcode 7.1 seems to think there are issues. It's giving me a stack trace saying that I'm modifying the autolayout engine from a background thread.

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.

Still not clear to me how to actually use this. The file is in my project and it appears in the Compile Build Phase. Do I need to import the file in the AppDelegate or somewhere and is there a call I need to make? I ask because it's not catching any problems but the new version of Xcode 7.1 seems to think there are issues. It's giving me a stack trace saying that I'm modifying the autolayout engine from a background thread.

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.

This comment has been minimized.

Minor improvements: Moved "#define PSPDFAssert(expression, ...) ...", commented "PSPDFLogError ..." out and added "#import <UIKit/UIKit.h>". If interested, you can pull from here: https://gist.github.com/drct/bb4b1f93f8790cca9c9f (since no pull requests are possible with gists.

Minor improvements: Moved "#define PSPDFAssert(expression, ...) ...", commented "PSPDFLogError ..." out and added "#import <UIKit/UIKit.h>". If interested, you can pull from here: https://gist.github.com/drct/bb4b1f93f8790cca9c9f (since no pull requests are possible with gists.

This comment has been minimized.

How to use it:@ponchorage just add the .m file (the one @drct enhanced) to your project, then after it is added to your project go to Build Phases -> Compile Sources and find the PSPDFUIKitMainThreadGuard.m double click it add the non-arc compiling flag. -fno-objc-arc
That is it.

How to use it:@ponchorage just add the .m file (the one @drct enhanced) to your project, then after it is added to your project go to Build Phases -> Compile Sources and find the PSPDFUIKitMainThreadGuard.m double click it add the non-arc compiling flag. -fno-objc-arc
That is it.