I have an application using Core Data with the following fairly standard hierarchy of managed entity contexts:
Persistent Store Coordinator β³ Save Context (Private Queue Concurrency Type) β³ Main Context (Main Queue Concurrency Type) β³ Private Context (Private Queue Concurrency Type)
The merge policy for all managed object contexts is set to NSMergeByPropertyObjectTrumpMergePolicy
I observe NSManagedObjectContextDidSaveNotification
, which is called by the following function when the private context is saved and merges the changes into the Main context:
func contextDidSaveNotificationHandler(notification: NSNotification) { if let savedContext = notification.object as? NSManagedObjectContext { if savedContext == privateObjectContext { mainObjectContext.performBlock({ if let updatedObjects = notification.userInfo![NSUpdatedObjectsKey] as? Set<NSManagedObject> {
This works most of the time, but sometimes I find that changes to existing objects in a private context are not combined into the Main context. I cannot understand why - maintaining a private context is successful; an NSManagedObjectContextDidSaveNotification
notification is sent; the notification handler is called; notification.userInfo?[NSUpdatedObjectsKey]
contains correctly updated objects; but at the end, the main context does not synchronize with the private context. (i.e., managed objects in the main context are not synchronized with the values ββcontained in notification.userInfo?[NSUpdatedObjectsKey]
). If I kill the application and restart it, the contexts are synchronized again (after loading objects from the persistent storage).
I have -com.apple.CoreData.ConcurrencyDebug 1
in my startup arguments, and all Core Data multithreading rules are followed. I do not see anything explicit in my hierarchy in the context of the managed entity or the merge function. What could be the reason for this?
source share