sedq sedq - 2 months ago 10x
iOS Question

MagicalRecord using

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


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


NSMutableArray *events = [NSMutableArray arrayWithArray:[Events MR_findAll]];

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


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 is 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 : %@", event)

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.