I have two NSManagedObjectContext
's, one is used to write data from an api response in the background, and the other is to populate a NSFetchedResultsController
as follows:
// moc used to fetch objects via NSFetchedResultsController:
moc = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
moc.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
// moc used when writing data from an api response:
backgroundMoc = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
moc.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
There are objects that are transiently stored in the background moc (without calling .save()
), and I need to merge those changes to over to the main moc so that my NSFetchedResultsController
has access to it.
I tried subscribing to NSManagedObjectContextDidSaveNotification
, but that only gets hit if i call backgroundMoc.save()
, which i am trying to avoid, since the objects in my backgroundMoc
have unsaved changes.
What is the best way to do so?
There's no built-in mechanism for this. When you have unsaved changes in one context, those changes only exist in that context. Other contexts can't see them, so they can't get the information they contain. If you don't want to save changes, your only option is to make the same changes again in the other context. That is, whatever you did in context A, do it again in context B so it has the same changes.