NSPersistentCloudkitContainer setup - merge behavior - contexts

Is it ok to use automaticallyMergesChangesFromParent to true for a managed object context that pins its query generation to current?

I have heard from a few sources that in most cases, it’s not recommended to set automaticallyMergesChangesFromParent to true for a managed object context that pins its query generation to current.

A source I have seen says:

When you pin a managed object context to a specific query generation, you’re explicitly telling the context to view the data as it existed at the time of that generation token (often referred to as a snapshot). This ensures that the context is isolated from any subsequent changes made by other contexts or background tasks.

But this may Conflict with Automatic Merging because:

The purpose of setting automaticallyMergesChangesFromParent to true is to have the context automatically merge changes from the parent (e.g., background or main context) into itself whenever the parent context saves changes. This behavior conflicts with the concept of a pinned query generation because:

•	If the context is pinned to current, it should not be concerned with changes that happen after that pinning.
•	Automatic merging would introduce updates from the parent context that occur after the pinning, thereby violating the “snapshot” nature of the query generation and potentially creating inconsistencies.

However, in your own Apple Sample Code Titled "" does use both together. Inside the definition of lazy var persistentContainer: NSPersistentCloudkitContainer the following code is included:


// Pin the viewContext to the current generation token, and set it to keep itself up to date with local changes.

container.viewContext.automaticallyMergesChangesFromParent = true

 do  {
        try container.viewContext.setQueryGenerationFrom(.current)

 }  catch {
           fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")

}

NSPersistentCloudkitContainer setup - merge behavior - contexts
 
 
Q