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?
A couple of thoughts: Looking at the code for MagicalRecord 3.0,
MR_context
returns aNSPrivateQueueConcurrencyType
. UsingMR_createEntity
appears to use a different, main queue context. Try creating your entity withMR_createEntityInContext:
and passing inMR_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: