Question

According to Apple's doc, we have the following:

When you save changes in a context, the changes are only committed “one store up.” If you save a child context, changes are pushed to its parent. Changes are not saved to the persistent store until the root context is saved. (A root managed object context is one whose parent context is nil.) In addition, a parent does not pull changes from children before it saves. You must save a child context if you want ultimately to commit the changes.

My data model roughly consists of the following NSManagedObject hierarchy:

Category <---->> Feed <---->> Post

My application, an RSS Reader, uses the following:

  • a "root" NSManagedObjectContext with a NSPrivateQueueConcurrencyType Concurrency type. I use this MOC to persist my changes to the NSPersistentStoreCoordinator.

  • a "main" NSManagedObjectContext with a NSMainQueueConcurrencyType Concurrency type. I use this MOC to feed my GUI.

  • a "local" NSManagedObjectContext with a NSPrivateQueueConcurrencyType Concurrency type. I use this MOC when creating batches of new Posts objects.

So, my questions are:

  1. When I save my localMOC, does it automatically propagate to my mainMOC ? or do I still have to observe NSManagedObjectContextDidSaveNotification from the localMOCand manually merge both MOCs with mergeChangesFromContextDidSaveNotification?
  2. Until yesterday, the batch import was taking place in an external DBOperation <NSOperation> context which was send to an NSOperationQueue, how would the synchronisation occur here? Do I have to pass the mainMOC as a parameter to DBOperation in order to use it as each localMOC's parent?
  3. I placed the batch routine back in my MainViewController but I am not sure it was a good idea. Should I stick to a NSOperationQueue like before or does the current [localMOC performBlock:^{ ... }]; structure offer me decent background processing?

Thanks in advance for your help.

Was it helpful?

Solution

I finally solved my problems by implementing my own save routine:

[_localMOC performBlockAndWait:^{
    NSError *errLoc=nil;

    if (![self.mainMOC obtainPermanentIDsForObjects:@[[[_mainMOC insertedObjects]     arrayByAddingObjectsFromArray:[_mainMOC updatedObjects]]] error:&errLoc]) {
    NSLog(@" ... ");
    }

    if (![_localMOC save:&errLoc]) {
      NSLog(@" ... ");
    }
    [_mainMOC performBlockAndWait:^{
        NSError *errMain=nil;
        if (![_mainMOC save:&errMain]) {
            NSLog(@" ... ");
        }
    }]
}];

Note that _mainMOC is being observed from the AppDelegate and will have its changes asynchronically persisted to disk by his _saveMOC parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top