9.5 C
New York
Tuesday, March 11, 2025

ios – CoreData + CloudKit Sync Points: Noticed Context Saves and Skipped Migrations


I’m utilizing CoreData with CloudKit in my iOS app, and I’ve encountered a collection of points whereas syncing knowledge. The logs present a number of context saves and migration skips, however there’s no clear indication of what is perhaps flawed. Beneath is an excerpt of the logs and the related CoreData setup code.

The logs repeatedly present the next:

CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:]_block_invoke_2(3216): 
 - Ignoring distant change notification as a result of it did not change any entities tracked by persistent historical past: 
 (URL: file:///var/cellular/Containers/Knowledge/Software/62AABD0C-972D-4477-BB81-DA2F385B5B43/Library/Applicationpercent20Support/CloudDataModel.sqlite)

And a number of other skipped migrations like:

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

Right here is the configuration for my NSPersistentCloudKitContainer in PersistenceController.swift:

lazy var cloudContainer: NSPersistentCloudKitContainer = {
    let container = NSPersistentCloudKitContainer(identify: "CloudDataModel")
    
    guard let description = container.persistentStoreDescriptions.first else {
        fatalError("Didn't retrieve retailer 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 shops: (error)")
        }
    }
    
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    container.viewContext.transactionAuthor = "integralstudios.kalo"
    container.viewContext.automaticallyMergesChangesFromParent = true
    
    do {
        attempt container.viewContext.setQueryGenerationFrom(.present)
        attempt container.initializeCloudKitSchema()
    } catch {
        print("⚠️ Didn't initialize CloudKit schema: (error)")
    }
    
    return container
}()

I additionally use this methodology to save lots of knowledge:

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

My very own logging is exhibiting the cloudkit performance working:

✅ CloudKit schema initialized efficiently
✅ Audio session configured efficiently
✅ Utilizing shared PersistenceController occasion
✅ Verified 6 meals in cloud context
✅ SharedDataManager: UserDefaults created
✅ SharedDataManager: Defaults registered
✅ SharedDataManager: Efficiently verified write entry

Additionally the icloud.developer DB viewer exhibits nothing out of the odd.

  • NSCloudKitMirroringDelegate is observing adjustments however skipping many notifications.
  • It seems that persistent historical past adjustments are usually not being tracked as anticipated.
  • Some migrations are skipped resulting from current columns.

Any insights, solutions, or comparable experiences are tremendously appreciated! Particularly something that helps me dissolve these error calls.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles