Reputation: 921
I need an answer from a reliable source, about saving NSManagedObjectContexts. Imagine I have a long writing task in in a global queue, I have two contexts as singleton reference (1 of NSMainQueueConcurrency, the other private)...
I have a large task of writing into database and my code look like this:
[privateMOC performBlock^:{
// Here goes a large writing task
................................
// And here goes saving
[privateMOC save:nil];
[mainMOC performBlock^:{
[mainMOC save:nil];
}]
}];
My question is: is it necessary to call save to main context inside a performBlock^
of a privateContext
, and another note is that there is no parent-child context structure, but there are two NSPersistentStoreCoordinators
.
Thx in advance :)
Upvotes: 0
Views: 277
Reputation: 70946
As usual, it depends what you want and need. :)
In general there's no requirement for one context's save call to be nested in a block passed to another context. You might want to do that in some cases, though. In your sample code, you guarantee that [mainMOC save:nil]
won't get called until after [privateMOC save:nil]
finishes. Is that important? There's not enough detail in your question to say if it matters in your case, so you'll have to consider that for yourself.
While I'm at it though, you really need to check the results of your save calls. The method returns a boolean to tell you if saving succeeded, and it can return an NSError
by reference to tell you why it failed. Your code doesn't check the result and doesn't allow for an NSError
-- meaning that if saving ever fails, you've ensured that you won't know that it failed or why. Use the information that the framework is trying to give you.
Upvotes: 1