-
-
Save torinkwok/e49fd8b5d97aae4b0710de4bce3c0cac to your computer and use it in GitHub Desktop.
This is a guard that tracks down AppKit/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 …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from the commercial iOS PDF framework http://pspdfkit.com. | |
// Copyright (c) 2016 Tong Kuo. All rights reserved. | |
// Licensed under MIT (http://opensource.org/licenses/MIT) | |
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved. | |
// Licensed under MIT (http://opensource.org/licenses/MIT) | |
// | |
#if !RELEASE | |
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it. | |
/* Define generic Target-prefixed macros for the view class that have slight naming variations across iOS and OS X, | |
* which allows the same code to be platform-independent. */ | |
#if TARGET_OS_IPHONE | |
# import <UIKit/UIKit.h> | |
# define TargetView UIView | |
#else | |
# import <Cocoa/Cocoa.h> | |
# define TargetView NSView | |
#endif | |
#import <objc/runtime.h> | |
#import <objc/message.h> | |
// Compile-time selector checks. | |
#if DEBUG | |
#define PROPERTY(propName) NSStringFromSelector(@selector(propName)) | |
#else | |
#define PROPERTY(propName) @#propName | |
#endif | |
#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) | |
#define PSPDFLogError NSLog | |
// http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html | |
BOOL PSPDFReplaceMethodWithBlock(Class c, SEL origSEL, SEL newSEL, id block) { | |
PSPDFAssert(c && origSEL && newSEL && block); | |
if ([c instancesRespondToSelector:newSEL]) return YES; // Selector already implemented, skip silently. | |
Method origMethod = class_getInstanceMethod(c, origSEL); | |
// Add the new method. | |
IMP impl = imp_implementationWithBlock(block); | |
if (!class_addMethod(c, newSEL, impl, method_getTypeEncoding(origMethod))) { | |
PSPDFLogError(@"Failed to add method: %@ on %@", NSStringFromSelector(newSEL), c); | |
return NO; | |
}else { | |
Method newMethod = class_getInstanceMethod(c, newSEL); | |
// If original doesn't implement the method we want to swizzle, create it. | |
if (class_addMethod(c, origSEL, method_getImplementation(newMethod), method_getTypeEncoding(origMethod))) { | |
class_replaceMethod(c, newSEL, method_getImplementation(origMethod), method_getTypeEncoding(newMethod)); | |
}else { | |
method_exchangeImplementations(origMethod, newMethod); | |
} | |
} | |
return YES; | |
} | |
SEL _PSPDFPrefixedSelector(SEL selector) { | |
return NSSelectorFromString([NSString stringWithFormat:@"pspdf_%@", NSStringFromSelector(selector)]); | |
} | |
#define PSPDF_ASSERT_TEMPLATE @"\nERROR: All calls to AppKit/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: %@" | |
void PSPDFAssertIfNotMainThread(void) { | |
// This gist is unhappy on some animation dense UI elements akin NSProgressIndicator. | |
// Looks like it does its UI work off the main thread. | |
// Stack trace displays that it does its animation work on a background thread labeled "com.apple.appkit-heartbeat" | |
BOOL isLegalThread = NSThread.isMainThread || [ [ NSThread currentThread ].name isEqualToString: @"com.apple.appkit-heartbeat" ]; | |
NSArray <NSString*>* stacktrace = NSThread.callStackSymbols; | |
PSPDFAssert( isLegalThread , PSPDF_ASSERT_TEMPLATE, stacktrace ); | |
} | |
__attribute__((constructor)) static void PSPDFGuiKitsMainThreadGuard(void) { | |
@autoreleasepool { | |
for (NSString *selStr in @[PROPERTY(setNeedsLayout), PROPERTY(setNeedsDisplay), PROPERTY(setNeedsDisplayInRect:)]) { | |
SEL selector = NSSelectorFromString(selStr); | |
SEL newSelector = NSSelectorFromString([NSString stringWithFormat:@"pspdf_%@", selStr]); | |
if ([selStr hasSuffix:@":"]) { | |
PSPDFReplaceMethodWithBlock(TargetView.class, selector, newSelector, ^(__unsafe_unretained TargetView *_self, CGRect r) { | |
// Check for window, since *some* AppKit/UIKit methods are indeed thread safe. | |
// https://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html | |
/* | |
Drawing to a graphics context in AppKit/UIKit is now thread-safe. Specifically: | |
The routines used to access and manipulate the graphics context can now correctly handle contexts residing on different threads. | |
String and image drawing is now thread-safe. | |
Using color and font objects in multiple threads is now safe to do. | |
*/ | |
if (_self.window) PSPDFAssertIfNotMainThread(); | |
((void ( *)(id, SEL, CGRect))objc_msgSend)(_self, newSelector, r); | |
}); | |
}else { | |
PSPDFReplaceMethodWithBlock(TargetView.class, selector, newSelector, ^(__unsafe_unretained TargetView *_self) { | |
if (_self.window) { | |
if (!NSThread.isMainThread) { | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
dispatch_queue_t queue = dispatch_get_current_queue(); | |
#pragma clang diagnostic pop | |
// iOS 8 layouts the MFMailComposeController in a background thread on an AppKit/UIKit queue. | |
// https://github.com/PSPDFKit/PSPDFKit/issues/1423 | |
if (!queue || !strstr(dispatch_queue_get_label(queue), "AppKit/UIKit")) { | |
PSPDFAssertIfNotMainThread(); | |
} | |
} | |
} | |
((void ( *)(id, SEL))objc_msgSend)(_self, newSelector); | |
}); | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment