Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested NSManagedObjectContext save and objectID

childContext and parentContext are both of "NSMainQueueConcurrencyType"

[childContext performBlock:^(void) {
  [childContext save:NULL];

  [parentContext performBlock:^(void) {
    [parentContext save:NULL];
    // Why is objectID for the inserted NSManagedObject still a temporary one here?
  }];
}];

Question:

  1. Is this the right way to save both the child and parent context
  2. Why is it that after saving, the inserted NSManagedObject's objectID is still a temporary one?
like image 274
Ronnie Liew Avatar asked Aug 08 '12 21:08

Ronnie Liew


1 Answers

I can think of a few rare occasions where you would want to have a main-queue MOC as a child of another main-queue MOC, but seeing that sure begs me to ask: What's the advantage of having both of them be NSMainQueueConcurrencyType?

Also, this is a known bug (at least it has been reported a number of times). When you insert and save from a child context, only the one directly connected to the persistent store gets its IDs mutated. So, when saving newly inserted items, you have several choices.

  1. Acquire persistent IDs before saving.
  2. Call [moc refreshObject:object mergeChanges:NO] and set all references to nil.
  3. Acquire persistent IDs after the most parent saves

I prefer #3 because it requires a dip into the database. If you do it after, it can get them from the parent.

like image 66
Jody Hagins Avatar answered Nov 15 '22 11:11

Jody Hagins