Hi I am having a problem creating entitys with magicalRecord in OSX, I create the model and the NSManagagedSubclasses and so on. But when i run this code in applicationDidFinishLaunching(I am using magicalRecord 2.2):
[MagicalRecord setupCoreDataStack];
Person * person = [Person MR_createEntity];
person.name = @"alex";
I get this in the log:
2013-10-06 18:01:54.320 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_contextWithStoreCoordinator:](0x7fff71a96238) -> Created Context UNNAMED
2013-10-06 18:01:54.321 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_setRootSavingContext:](0x7fff71a96238) Set Root Saving Context: <NSManagedObjectContext: 0x101924d00>
2013-10-06 18:01:54.321 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_newMainQueueContext](0x7fff71a96238) Created Main Queue Context: <NSManagedObjectContext: 0x1001564a0>
2013-10-06 18:01:54.321 TestMagicalRecord[7554:303] +[NSManagedObjectContext(MagicalRecord) MR_setDefaultContext:](0x7fff71a96238) Set Default Context: <NSManagedObjectContext: 0x1001564a0>
2013-10-06 18:01:54.322 TestMagicalRecord[7554:303] +entityForName: could not locate an entity named '(null)' in this model.
When i run the same code in an IOS project it runs with no problems, i have been trying to find sample code for OSX but with no luck, should it be any different?
The Mac sample code in the Magical record Github repo does only contain a project is there a way to run it with the other resources?
I bet its a simple solution but i cant find anything in the Docs/SO that will work.
As you also found out in the mean time, this is a bug in the Development branch of Magical Record.
In the 2.2 version of the Development branch, MR_createEntity uses the following method to determine the entity name of the object that should be created:
+ (NSString *) MR_bestGuessAtAnEntityName
{
if ([self respondsToSelector:@selector(entityName)])
{
return [self performSelector:@selector(entityName)];
}
return NSStringFromClass(self);
}
But (at least on OS X 10.8), NSObject actually responds to entityName, and invoking
that method returns nil. I could not find any documentation on this, so this might be
some internal undocumented method.
But the effect is that MR_bestGuessAtAnEntityName returns nil, and therefore
+ (id) MR_createInContext:(NSManagedObjectContext *)context
{
return [NSEntityDescription insertNewObjectForEntityForName:[self MR_bestGuessAtAnEntityName] inManagedObjectContext:context];
}
fails with the error message
+entityForName: could not locate an entity named '(null)' in this model.
So this is a Magical Record bug, and is worth reporting to the maintainers.
The bug occurs only on OS X, because NSObject (or NSManagedObject) does not respond to entityName on iOS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With