MagicalRecord 3.0 not saving. No objects for the second build

439 Views Asked by At

I am trying out MagicalRecord 3.0 for the first time and I can't make it work.

This is what I am doing.

1- Setup stack:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[MagicalRecord setupClassicStackWithSQLiteStoreNamed:@"Model"];}

My model name is "Model.xcdatamodeld".

2- Create entities and Save:

Preferences *p = [Preferences createEntity];
p.visibilityWindow = @"08:00-23:30";

[[NSManagedObjectContext MR_context] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
    if (success) {
        NSLog(@"You successfully saved your context.");
    } else if (error) {
        NSLog(@"Error saving context: %@", error.description);
    }
}];

3- Load data:

NSArray *matches;

NSError * error = nil;
matches = [Preferences MR_findAll];

Preferences *p = nil;

NSLog(@"Fetch request data %@",matches);
if(!matches || error || ([matches count] >1)){
    //handle error;
    NSLog(@"Error %@ matches count %lu", error, (unsigned long)[matches count]);
}else if ([matches count]){
    p = [matches firstObject];
    NSLog(@"Preferences found in coredata %@", p);
}else{
    NSLog(@"No matches %i", [matches count]);
}

I am also "Cleaning up" as the documentation suggests:

- (void)applicationWillTerminate:(UIApplication *)application {
[MagicalRecord cleanUp];

}

It was working totally fine when I was using the traditional core data framework.

It says that I am saving successfully. But when I quit the app and try to run it again it doesn't work anymore. What am I doing wrong?

Also, from the posts I read, everyone talks about a "MR_defaultContext". Was it deprecated?

enter image description here

2

There are 2 best solutions below

4
On BEST ANSWER

A couple of thoughts: Looking at the code for MagicalRecord 3.0, MR_context returns a NSPrivateQueueConcurrencyType. Using MR_createEntity appears to use a different, main queue context. Try creating your entity with MR_createEntityInContext: and passing in MR_context.

Also, without knowing when you are calling each of the three methods, try using MR_saveToPersistentStoreAndWaitWithError:.

EDIT: Probably the quickest thing to do to test is to change step 2:

Preferences *p = [Preferences createEntity];
p.visibilityWindow = @"08:00-23:30";

NSError *error; // add this
BOOL success = [p.managedObjectContext MR_saveToPersistentStoreWithError:error] // change this
if (success) {
    NSLog(@"You successfully saved your context.");
} else if (error) {
    NSLog(@"Error saving context: %@", error.description);
}
0
On

I had the same problem... there is that I used (with your stuff) also I add the option to store with UIBackgroundTaskIdentifier (opcional)

--

Preferences *p = [Preferences createEntity];
p.visibilityWindow = @"08:00-23:30";

[SomethingManagerObject saveIntoContext]

--

SomethingManagerObject

+ (void) saveWithContext{

//  THIS IS opcional!!! -> Prepar for BackgroundTask
UIApplication * application = [UIApplication sharedApplication];

__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

//  Get the context
NSManagedObjectContext *localContext = [[MagicalRecordStack defaultStack] context];

// Save the modification in the context
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;

}];
}

Or you can save with managedObjectContext of the entity. In saveWithContext method just add parameter and send the p.managedObjectContext. Inside change [[MagicalRecordStack defaultStack] context]

The SomethingManagerObject is a NSObject that you can use to have class methods like that... save...create...