Can't set NSMergePolicy to my view context

37 views Asked by At

Here is my CoreData container setup code:

private func _setupContainer() {
    
    persistentContainer = .init(name: "MyAppName")
    
    let storeDescription = persistentContainer.persistentStoreDescriptions.first!
    storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
    
    persistentContainer.loadPersistentStores { _, error in
        if let error {
            fatalError("Failed to load persistent container: \(error)")
        }
        Log.info("Did load CoreData store")
    }
    
    Log.debug("Step 1")
    
    persistentContainer.viewContext.mergePolicy = NSMergePolicy.overwrite // NSMergeByPropertyObjectTrumpMergePolicy
    
    Log.debug("Step 2")
    
    mainContext.undoManager = nil
    mainContext.shouldDeleteInaccessibleFaults = true
    mainContext.automaticallyMergesChangesFromParent = true
    
    Log.debug("Did setup CoreData container")
}

I got the "Did load CoreData store" log message well, but the app is blocked after "Step 1", when assigning the merge policy of the view context. (And I have the same issue with any other merge policy).

If I remove the line, the app launches successfully.

What am I doing wrong?

0

There are 0 answers