Unable to save context with -(void)save, seems to work only with -(void)saveNestedContextsErrorHandler #179

tanissian opened this Issue Jun 7, 2012 · 8 comments

6 participants


I am seeing this on branch 2.0 with SDK 5.1, ARC enabled.

The app itself is dead simple, just inserting a bunch of new records and save them to store.

I am creating a context with [NSManagedObjectContext contextForCurrentThread], then I do [context save]. The save is successful but the records are not saved into the store. Next time I restart the app, the fetch will return 0 result set. The sqlite db file remains unchanged as well (same checksum).

If I use [context saveNestedContextsErrorHandler:^(NSError *err){}], then the inserted data seems to get saved into the store.

I have tried this using both main thread and dispatched low priority thread with no luck.

Am I missing something? Any help is appreciated! Thanks.


Thanks for the clarification, really appreciate it!

So it sounds like calling -(void)save on default context should trigger a parent context to save?

I am using contextForCurrentThread on main thread, and I believe I do get back the default context. However, calling -(void)save on that context did not seem to push the data into store.

In the log, I do see these lines:

2012-06-07 14:35:23.978 abc[39967:fb03] -NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback: -> Saving : *** DEFAULT *** Context *** MAIN THREAD ***
2012-06-07 14:35:23.986 abc[39967:13c03] -NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback: -> Saving : *** BACKGROUND SAVE *** Context


@magicalpanda as I understood you mean that call save on default context will save it anyway? So than I couldn't understand why I got this issue: #174 I called save on default context. And it's actually stores to sqlite database! I cheched it by opening sqlite database by hands. But in my application it doesn't update my item to correct state. I couldn't use it in predicates in fetch requests directly after saving it (after creating entity and calling save on defaultContext). After applicatiuon restart it is works! But directly after creating new entity and saving - it doesn't work. Please pay attention also to my ticket #174 I even created test app with two branches. One uses MagicalRecord 1.8.* and another one 2.0.3. In 1.8 everything works ok, but not in 2.0.3( It seems like object doesn't updated in its context or maybe cached somewhere... Even objectId not changing after context save.


Thanks @magicalpanda , now I understand the setup in MagicalRecord better. I will revisit my code and double check, but from what I have been seeing, saving on default context does not seem to persist the data onto disk.


@casademora sorry for delay with the answer) Please, let's continue discussion)

I say this because there are obviously a number of ways to set up a saving context

But I like MagicalRecord! I think that you did great work and I would like to use your cool library) But one little thing make some difficulties to me. I think that you are very competent in this area and if you did something in some way so it means that there were reasons for this. I'm coding in objective-c less than a year, so I could not understand some things. I tried to understand how MagicalRecords works and its goals. After this conversation it seems that I begun to understand it a little better) But a few questions still exists)

So, please give me an advice what to do in my situation. In simple words: I need to create an instance and after that to use it in predicate in fetchRequest. So for this purpose it should be updated in its managedObjectContext after inserting into persistent store. How to do this?

In case when I call simple save even of defaultManagedObjectContext it will be saved in background on special queue. And it will not be updated in defaultManagedObjectContext. So one solution I found is to create new context with [NSManagedObjectContext context] and perform fetch in that new context

[[Photo findAllWithPredicate:predicate inContext:[NSManagedObjectContext context]]

But I think that it's not very nice solution to create managedObjectContext each time.

Another possible solution is to save context with completion with saveInBackgroundCompletion:. (BTW: maybe better to remive it to saveWithCompletion: because now saving only in background. And existence of such method made me a little mislead). In case of using this method I have to perform fetchRequest in completion block.

We this two solutions it us possible to solve my task. Below I will explain two

  1. freeze while saving. How about adding extra method for saving to MagicalRecord which will freeze thread untill it willn't be saved? or it is a bad idea?

  2. is it possible to call some kind of synchronization to current context? As I remember after saving my new object in background thread (after calling [[NSManagedObjectContext defaultContext] save]) this object will not be updated in its defaultContext. So it's not possible to use it in predicate because it will have bat objectID. If you want I could update my demo project to demonstrate this. Or it's not a good solution?

@casademora thank you in advance and sorry for taking so much your time) Some day I will be competent enough (I hope =) ) to help you with contributions to MagicalRecord)))


I also ran into the problem of MR/CD not persisting to the sqlite file. This seems to fix the issue in my project:
[[NSManagedObjectContext contextForCurrentThread] MR_saveNestedContexts];


it seems that this issue could be closed too.

@casademora casademora was assigned Dec 14, 2012
@tonyarnold tonyarnold closed this Dec 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment