Greg_R
Greg_R

Reputation: 1

CoreData + CloudKit Sync Issues: Observed Context Saves and Skipped Migrations

I am using CoreData with CloudKit in my iOS app, and I have encountered a series of issues while syncing data. The logs show multiple context saves and migration skips, but there’s no clear indication of what might be wrong. Below is an excerpt of the logs and the relevant CoreData setup code.

The logs repeatedly show the following:

CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:]_block_invoke_2(3216): 
<NSCloudKitMirroringDelegate: 0x3000b02d0> - Ignoring remote change notification because it didn't change any entities tracked by persistent history: 
<NSSQLCore: 0x1041743c0> (URL: file:///var/mobile/Containers/Data/Application/62AABD0C-972D-4477-BB81-DA2F385B5B43/Library/Application%20Support/CloudDataModel.sqlite)

And several skipped migrations like:

CoreData: debug: CoreData+CloudKit: -: 
Skipping migration for 'ANSCKRECORDMETADATA' because it already has a column named 'ZNEEDSUPLOAD'

Here is the configuration for my NSPersistentCloudKitContainer in PersistenceController.swift:

lazy var cloudContainer: NSPersistentCloudKitContainer = {
    let container = NSPersistentCloudKitContainer(name: "CloudDataModel")
    
    guard let description = container.persistentStoreDescriptions.first else {
        fatalError("Failed to retrieve store description")
    }
    
    description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
    description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
    
    let containerIdentifier = "iCloud.com.integralstudios.kalo"
    description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier)
    
    container.loadPersistentStores { description, error in
        if let error = error {
            print("❌ Unable to load cloud persistent stores: \(error)")
        }
    }
    
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    container.viewContext.transactionAuthor = "integralstudios.kalo"
    container.viewContext.automaticallyMergesChangesFromParent = true
    
    do {
        try container.viewContext.setQueryGenerationFrom(.current)
        try container.initializeCloudKitSchema()
    } catch {
        print("⚠️ Failed to initialize CloudKit schema: \(error)")
    }
    
    return container
}()

I also use this method to save data:

func save() {
    let localContext = localContainer.viewContext
    let cloudContext = cloudContainer.viewContext
    
    if localContext.hasChanges {
        do {
            try localContext.save()
        } catch {
            print("Error saving local context: \(error)")
        }
    }
    
    if cloudContext.hasChanges {
        do {
            try cloudContext.save()
        } catch {
            print("Error saving cloud context: \(error)")
        }
    }
}

My own logging is showing the cloudkit functionality working:

✅ CloudKit schema initialized successfully
✅ Audio session configured successfully
✅ Using shared PersistenceController instance
✅ Verified 6 meals in cloud context
✅ SharedDataManager: UserDefaults created
✅ SharedDataManager: Defaults registered
✅ SharedDataManager: Successfully verified write access

Also the icloud.developer DB viewer shows nothing out of the ordinary.

Any insights, suggestions, or similar experiences are greatly appreciated! Especially anything that helps me dissolve those error calls.

Upvotes: 0

Views: 22

Answers (0)

Related Questions