GrahamDennis / GDCoreDataConcurrencyDebugging

GDCoreDataConcurrencyDebugging helps you find cases where NSManagedObject's are being called on the wrong thread or dispatch queue.
MIT License
170 stars 23 forks source link

Error when use NSPrivateQueueConcurrencyType in main thread. #2

Closed kostiakoval closed 11 years ago

kostiakoval commented 11 years ago

There is an empty project with just one function in the appDelegate for test -

- (void)test
{
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    Event *a = [Event insertInManagedObjectContext:context];
}

Error messages in the console

2013-10-11 16:40:42.567 Festival[87700:c07] Invalid concurrent access to managed object calling 'autorelease'; Stacktrace: ( 0 Festival 0x00005767 ValidateConcurrency + 183 1 Festival 0x00005400 CustomSubclassAutorelease + 48 2 CoreData 0x000aa0af +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 143 3 Festival 0x000036b2 +[_Event insertInManagedObjectContext:] + 322 4 Festival 0x000031d3 -[RFAppDelegate test] + 195 5 Festival 0x000032d7 -[RFAppDelegate application:didFinishLaunchingWithOptions:] + 151 6 UIKit 0x00969157 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 266 7 UIKit 0x00969747 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1248 8 UIKit 0x0096a94b -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 805 9 UIKit 0x0097bcb5 -[UIApplication handleEvent:withNewEvent:] + 1022 10 UIKit 0x0097cbeb -[UIApplication sendEvent:] + 85 11 UIKit 0x0096e698 _UIApplicationHandleEvent + 9874 12 GraphicsServices 0x0270adf9 _PurpleEventCallback + 339 13 GraphicsServices 0x0270aad0 PurpleEventCallback + 46 14 CoreFoundation 0x0156fbf5 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 53 15 CoreFoundation 0x0156f962 CFRunLoopDoSource1 + 146 16 CoreFoundation 0x015a0bb6 CFRunLoopRun + 2118 17 CoreFoundation 0x0159ff44 CFRunLoopRunSpecific + 276 18 CoreFoundation 0x0159fe1b CFRunLoopRunInMode + 123 19 UIKit 0x0096a17a -[UIApplication _run] + 774 20 UIKit 0x0096bffc UIApplicationMain + 1211 21 Festival 0x00002a9d main + 141 22 libdyld.dylib 0x02442725 start + 0 ) 2013-10-11 16:40:42.569 Festival[87700:c07] Invalid concurrent access to managed object calling 'autorelease'; Stacktrace: ( 0 Festival 0x00005767 ValidateConcurrency + 183 1 Festival 0x00005400 CustomSubclassAutorelease + 48 2 libobjc.A.dylib 0x01431d53 objc_autorelease + 51 3 Festival 0x000036eb +[_Event insertInManagedObjectContext:] + 379 4 Festival 0x000031d3 -[RFAppDelegate test] + 195 5 Festival 0x000032d7 -[RFAppDelegate application:didFinishLaunchingWithOptions:] + 151 6 UIKit 0x00969157 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 266 7 UIKit 0x00969747 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1248 8 UIKit 0x0096a94b -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 805 9 UIKit 0x0097bcb5 -[UIApplication handleEvent:withNewEvent:] + 1022 10 UIKit 0x0097cbeb -[UIApplication sendEvent:] + 85 11 UIKit 0x0096e698 _UIApplicationHandleEvent + 9874 12 GraphicsServices 0x0270adf9 _PurpleEventCallback + 339 13 GraphicsServices 0x0270aad0 PurpleEventCallback + 46 14 CoreFoundation 0x0156fbf5 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 53 15 CoreFoundation 0x0156f962 CFRunLoopDoSource1 + 146 16 CoreFoundation 0x015a0bb6 CFRunLoopRun + 2118 17 CoreFoundation 0x0159ff44 CFRunLoopRunSpecific + 276 18 CoreFoundation 0x0159fe1b CFRunLoopRunInMode + 123 19 UIKit 0x0096a17a -[UIApplication _run] + 774 20 UIKit 0x0096bffc UIApplicationMain + 1211 21 Festival 0x00002a9d main + 141 22 libdyld.dylib 0x02442725 start + 0

GrahamDennis commented 11 years ago

GDCoreDataConcurrencyDebugging is correctly reporting this as a problem. You must do all interaction with contexts and managed objects on the relevant queue. So the safe version of this code is:

[context performBlockAndWait:^{
    Event *event = [Event insertInManagedObjectContext:context];
}];
kostiakoval commented 11 years ago

Thanks for so quick reply. Great debugging tool