MR_save seems to be breaking my context and making NSManagedObject disappear #245

Closed
alimpaecher opened this Issue Sep 6, 2012 · 2 comments

Comments

Projects
None yet
2 participants

I am using magical record to handle core data. I have code that creates a Entity which looks as follows:

if ( gameEntity == nil )
{    
    gameEntity = [GameEntity MR_createEntity];
}
gameEntity.opponent = opponent;
gameEntity.me = me;
etc...

When I then query for game entities as follows:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF IN %@ AND gameState != %@", me.myGames, GAME_ENTITY_STATE__OTHER];
NSPredicate* gamePredicate = [GameEntity predicateForGamesWithPredicateType:gameEntityPredicateType];
NSFetchRequest* fetchRequest = [GameEntity MR_requestAllWithPredicate:gamePredicate];
NSArray * results = [[NSManagedObjectContext MR_defaultContext] executeFetchRequest:fetchRequest error:nil];
return results;

Results has a size of 1 (which makes sense).

However when I save the context as follows:

[[NSManagedObjectContext MR_defaultContext] MR_save];

And run the same fetchRequest above, all of a sudden the GameEntity is gone and there are no results in NSArray.

Even weirder when I shut down the app and run it again, it finds the GameEntity. So it was in fact saved. Why is MR_save essentially breaking my NSManagedObject context?

All the save and creates are being run on the main thread.

@ghost ghost assigned tonyarnold Dec 14, 2012

Contributor

tonyarnold commented Dec 14, 2012

We're working on changing the way saves work to make things a lot simpler in an upcoming release, but in the meantime, could you please try using MR_saveNestedContexts in place of MR_save? That should make the saves persist to disk as you're expecting them to.

Contributor

tonyarnold commented Jan 9, 2013

Changes to the save methods have been released as part of MagicalRecord 2.1, which is available now. We've also changed all completion blocks so that they dispatch onto the main thread — updating to this new version and using the new save methods should resolve the issues that you were seeing.

Feel free to reopen this issue if you continue to see the same problems.

@tonyarnold tonyarnold closed this Jan 9, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment