Search code examples
ioscore-datamagicalrecord

MagicalRecord using


I'm new to CoreData and MR. Trying to save some entities and read them after.

saving:

Events *newEvent = [Events MR_createEntity];
newEvent.title = @"qwe";
newEvent.date = [NSDate date];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL contextDidSave, NSError * _Nullable error) {}];

reading:

NSMutableArray *events = [NSMutableArray arrayWithArray:[Events MR_findAll]];
NSLog(@"%@",events);

as result I'm getting "data: < fault >"

if I add private context like:

NSManagedObjectContext *context = [NSManagedObjectContext MR_newPrivateQueueContext];
NSMutableArray *events = [NSMutableArray arrayWithArray:[Events MR_findAllInContext:context]];

my app crashes with error reason: '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Events''

Can someone show me code working for my task


Solution

  • You don't need to add any private context for this (if you don't need it for other reasons, obvious). The "data: <fault>" is part of the iOS. Core Data doesn't extract the information of an object if they are not directly accessed, this is a good choice for performance reasons. So, if you want to print in console your array, you have to cycle it and print every single element extracting it from the array.

    for (Event *event in [Events MR_findAll]) {
        NSLog(@"Event name : %@", event.name)
    }
    

    This should work good.

    PS: A little advice, use singular names for your entities because they represent a single object, a single class. Don't think at them as tables because they are not.