Skip to content
Browse files

Now erroring when you call performBlockAndWait__DISPATCH_SYNC_TO_MAIN…

…_IS_NOW_FORBIDDEN__ from the main thread, known to cause deadlocks and generally be a bad idea.
  • Loading branch information...
1 parent fa2c857 commit 9ab4b1eea5105557940d563cdc2fbb96caaa6fc7 @drewcrawford committed
Showing with 13 additions and 3 deletions.
  1. +5 −1 CoreDataHelp/CDHRepo.h
  2. +5 −0 CoreDataHelp/CDHRepo.m
  3. +2 −1 CoreDataHelp/CoreDataHelpError.h
  4. +1 −1 CoreDataHelp/CoreDataHelpError.m
View
6 CoreDataHelp/CDHRepo.h
@@ -79,7 +79,11 @@ typedef void(^operationBlock)();
*/
-(void) performBlockAndReturnOnMain:(operationBlock) b;
-/**This is like performBlock, but it waits for the operation to complete. You **MAY NOT** dispatch_sync onto the main thread inside this block. It's recommdended that you not use this method unless absolutely necessary. */
+/**This is like performBlock, but it waits for the operation to complete. You **MAY NOT** dispatch_sync onto the main thread inside this block. It's recommdended that you not use this method unless absolutely necessary.
+
+ You **MAY NOT** call this method from the main thread, or a queue that was joined to the main thread (e.g. via dispatch_sync). You should only call this method from a background queue.
+
+ */
-(void) performBlockAndWait__DISPATCH_SYNC_TO_MAIN_IS_NOW_FORBIDDEN__:(operationBlock) b;
/**This function must be run from a block. */
View
5 CoreDataHelp/CDHRepo.m
@@ -9,6 +9,7 @@
#import "CDHRepo.h"
#import <DCAKit/DCAKit.h>
#import "CoreDataStack.h"
+#import "CoreDataHelpError.h"
static CDHRepo *rootRepo;
static NSMapTable *reposByMoc;
@@ -101,6 +102,10 @@ -(void) internalPerformBlock:(operationBlock) b {
}
-(void) performBlockAndWait__DISPATCH_SYNC_TO_MAIN_IS_NOW_FORBIDDEN__:(operationBlock)b {
+ if ([NSThread isMainThread]) {
+ NSError *err = [CoreDataHelpError errorWithCode:CDHErrorCodeDeprecated format:@"You're calling performBlockAndWait__DISPATCH_SYNC_TO_MAIN_IS_NOW_FORBIDDEN__ on the main thread, which is known to cause deadlocks. This will crash in a future release."];
+ [err present];
+ }
[_moc performBlockAndWait:^{
[self internalReallyPerformBlock:b];
}];
View
3 CoreDataHelp/CoreDataHelpError.h
@@ -13,7 +13,8 @@ static const NSString *CDHErrorDomain __attribute__((used)) = @"com.dca.CoreData
typedef enum {
CDHErrorCodeNotSupported,
CDHErrorCodeHTTP,
- CDHErrorCacheTooOld
+ CDHErrorCacheTooOld,
+ CDHErrorCodeDeprecated
} CDHErrorCode;
View
2 CoreDataHelp/CoreDataHelpError.m
@@ -14,6 +14,6 @@ + (NSError *)errorWithCode:(CDHErrorCode)code format:(NSString *)format, ... {
va_start(args,format);
NSString *errorStr = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
- return [NSError errorWithDomain:(NSString*) CDHErrorDomain code:code userInfo:[NSDictionary dictionaryWithObject:errorStr forKey:@"errorString"]];
+ return [NSError errorWithDomain:(NSString*) CDHErrorDomain code:code userInfo:[NSDictionary dictionaryWithObject:errorStr forKey:NSLocalizedFailureReasonErrorKey]];
}
@end

0 comments on commit 9ab4b1e

Please sign in to comment.
Something went wrong with that request. Please try again.