简体   繁体   中英

What happens with child context when reseting it, meanwhile parent was updated?

If I create a parent / child context like below and recommended by CoreData guru Marcus Zarra :

// create writer MOC
_privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_privateWriterContext setPersistentStoreCoordinator:_persistentStoreCoordinator];

// create main thread MOC
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.parentContext = _privateWriterContext;

and for certain reason I am adding changes to _managedObjectContext and _privateWriterContext too, and save only _privateWriterContext , and latter on I reset _managedObjectContext , then _managedObjectContext will reset to _privateWriterContext updated state?

rollback would have the same effect?

Let's consider a situation as above that you have a private queue context which is attached to persistent store. Now, you create a child context from it, insert some object to this child context. When you save the child context and propagate the changes to the parent context to save it, the changes are already saved to persistent store.

Now, at this time resetting means simply nothing, it just clears the managed object contexts cache. So, if you reset the child context, then the cache that it has will be cleared. That also means that, if you do the fetch on this child context again, all those changes that were saved, will show again. If your purpose is to undo the changes in managedObjectContext, NSManagedObjectContext class provides undo , rollback methods that you can use.

Resetting simply means nothing. Every managed object has its own cache, which keeps track of all the fetched object in its memory, so if you reset a managedObjectContext, all the objects from its memory gets cleared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM