nil NSManagedObjectContext in saveDataWithBlock: #106

tonyarnold opened this Issue Dec 11, 2011 · 5 comments


None yet
2 participants

tonyarnold commented Dec 11, 2011

I'm seeing some really unusual behaviour — I'm executing a standard MRCoreDataAction saveDataWithBlock: method within one of my view controllers before I pop back to the parent, but the localContext variable is nil. I've traced it all the way back up, and it's nil all the way into the MR_defaultContext method as well. Is this a known issue? Am I doing something wrong?

Here's the exception stack trace:

2011-12-11 20:25:54.198 RedactoThisAppNamo[53641:17903] *** Assertion failure in +[DWKEntry insertInManagedObjectContext:], /Users/tonyarnold/Desktop/RedactoThisAppNamo/Data Model/Private/_DWKEntry.m:31
Current language:  auto; currently objective-c
(gdb) bt
#0  0x02515cf0 in objc_exception_throw ()
#1  0x0232ca78 in +[NSException raise:format:arguments:] ()
#2  0x00aa52db in -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] ()
#3  0x00004e7d in +[_DWKEntry insertInManagedObjectContext:] (self=0xa1d40, _cmd=0x7552528, moc_=0x0) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Data Model/Private/_DWKEntry.m:31
#4  0x02385d81 in +[NSObject performSelector:withObject:] ()
#5  0x0002b24e in +[NSManagedObject(MagicalRecord) MR_createInContext:] (self=0xa1d40, _cmd=0x8274f, context=0x0) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Vendor/MagicalRecord/Categories/NSManagedObject+MagicalRecord.m:651
#6  0x00013793 in __34-[DWKBookEditViewController save:]_block_invoke_0 (.block_descriptor=0xbfffdd58, localContext=0x0) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Classes/DWKBookEditViewController.m:262
#7  0x000218c2 in +[MRCoreDataAction saveDataWithBlock:errorHandler:] (self=0xa213c, _cmd=0x83fb6, block=0xbfffdd58, errorHandler=0x0) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Vendor/MagicalRecord/MRCoreDataAction.m:66
#8  0x00021a1a in +[MRCoreDataAction saveDataWithBlock:] (self=0xa213c, _cmd=0x828b1, block=0xbfffdd58) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Vendor/MagicalRecord/MRCoreDataAction.m:80
#9  0x00013520 in -[DWKBookEditViewController save:] (self=0x88f2170, _cmd=0x754e738, sender=0x8893830) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Classes/DWKBookEditViewController.m:252
#10 0x02385ec9 in -[NSObject performSelector:withObject:withObject:] ()
#11 0x0011f5c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#12 0x0035ad54 in -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] ()
#13 0x02385ec9 in -[NSObject performSelector:withObject:withObject:] ()
#14 0x0011f5c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#15 0x0011f55a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#16 0x001c4b76 in -[UIControl sendAction:to:forEvent:] ()
#17 0x001c503f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#18 0x001c42fe in -[UIControl touchesEnded:withEvent:] ()
#19 0x00144a30 in -[UIWindow _sendTouchesForEvent:] ()
#20 0x00144c56 in -[UIWindow sendEvent:] ()
#21 0x0012b384 in -[UIApplication sendEvent:] ()
#22 0x0011eaa9 in _UIApplicationHandleEvent ()
#23 0x0226efa9 in PurpleEventCallback ()
#25 0x022bd022 in __CFRunLoopDoSource1 ()
#26 0x022bb90a in __CFRunLoopRun ()
#27 0x022badb4 in CFRunLoopRunSpecific ()
#28 0x022baccb in CFRunLoopRunInMode ()
#29 0x0226d879 in GSEventRunModal ()
#30 0x0226d93e in GSEventRun ()
#31 0x0011ca9b in UIApplicationMain ()
#32 0x00004198 in main (argc=1, argv=0xbffff604) at /Users/tonyarnold/Desktop/RedactoThisAppNamo/Supporting Files/main.m:16

ghost commented Dec 11, 2011

Hrm, I don't think it should be calling performSelector in the create method...can you see which selector is being performed prior to crashing?


tonyarnold commented Dec 11, 2011

I'm using mogenerator generated NSManagedObject subclasses, so the performSelector call is calling on + (id)insertInManagedObjectContext:(NSManagedObjectContext*)moc_;


tonyarnold commented Dec 11, 2011

Interestingly, the NSManagedObjectContext is nil well before this point — it's certainly missing inside:

+ (id) MR_createInContext:(NSManagedObjectContext *)context;

and tracing back up it's not present in:

+ (void) saveDataWithBlock:(void (^)(NSManagedObjectContext *localContext))block errorHandler:(void (^)(NSError *))errorHandler;

tonyarnold commented Dec 13, 2011

This is absolutely my fault. I had my cleanup routines in:

- (void)applicationWillResignActive:(UIApplication *)application;

Massive brain fart. Sorry for wasting time on this one.

tonyarnold closed this Dec 13, 2011


ghost commented Dec 13, 2011

Hey, no worries, I'm glad you found the cause :)
For future reference, if you do need to use the cleanup routines, it's probably best to put them in willTerminate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment