问题
I am getting an exception with MagicalRecord version 2.0.7
whenever I launch the app, it crashes with this exception:
*** Assertion failure in +[NSManagedObjectContext MR_defaultContext]
I am configuring coredata like this:
In appdelegate didFinishLaunchingWithOptions method:
[MagicalRecord setupCoreDataStack];
In appdelegate applicationWillTerminate:
[MagicalRecord cleanUp];
I noticed the crash occurs only when I try to truncate all records from an entity:
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[Song MR_truncateAllInContext:localContext];
Am I missing something? is the method adopted above wrong ?
Edit
The crash occurs on this line:
+ (NSManagedObjectContext *) MR_defaultContext
{
@synchronized (self)
{
NSAssert(defaultManagedObjectContext_ != nil, @"Default Context is nil! Did you forget to initialize the Core Data Stack?");//this line
return defaultManagedObjectContext_;
}
}
In NSMnagedObjectContext+MagicalRecord.m file
回答1:
put your core data set up [MagicalRecord setupCoreDataStackWithStoreNamed:@"Model"];
on top of app delegate method.
回答2:
So, there is something else going in if you're specifying a context (the localContext) and you're crashing in the defaultContext accessor. The default context should not be nil after setting up the stack. Have you verified that on your own after initialization? Try using
+[MagicalRecord currentStack]
to see if everything has been set up properly.
One thing to check, in the truncateAllInContext: method, is that ignoring your local context and just asking for the default? If that's the case, then this is a bug in MagicalRecord, and may be fixed in a newer version (the latest is 2.1)
回答3:
Yes, upgrade MagicalRecord, if you can. I ran into various issues that had gone away with 2.1. At least one of which was a bug in Core Data that MagicalRecord managed to tickle.
来源:https://stackoverflow.com/questions/17918169/assertion-failure-in-nsmanagedobjectcontext-mr-defaultcontext-when-trying