1

I have an app in production using Core Data + CloudKit, synced using the built in NSPersistentCloudKitContainer.

It works perfectly well most of the time, but sometimes it simply won't sync with no errors or anything.

Some users report that it works perfectly from iPad to iPhone, but not from iPhone to iPad. Some users even report randomly losing data. I've checked thoroughly for bugs but I really can't figure it out. Has anyone had issues with this? Are there any solutions or at least things to try? Here's the data container code:

lazy var container: NSPersistentContainer = {
            
    let container = NSPersistentCloudKitContainer(name: "<AppName>")
    
    let directory = FileManager.default.urls(
        for: .applicationSupportDirectory, in: .userDomainMask).first!
    
    //Local data
    let localStoreDescription = NSPersistentStoreDescription(url: directory.appendingPathComponent("Local.sqlite"))
    localStoreDescription.configuration = "Local"
    
    //Cloud synced data
    let cloudStoreDescription = NSPersistentStoreDescription(
        url: directory.appendingPathComponent("Cloud.sqlite"))
    cloudStoreDescription.configuration = "Cloud"
    cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(
        containerIdentifier: "<ContainerIdentifier>")
    
    container.persistentStoreDescriptions = [
        cloudStoreDescription,
        localStoreDescription
    ]
    
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    
    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    
    return container
}()
pkamb
  • 33,281
  • 23
  • 160
  • 191
Miles
  • 487
  • 3
  • 12
  • https://stackoverflow.com/questions/67269379/core-data-and-cloudkit-synch-is-inconsistent – pkamb Oct 08 '21 at 22:06
  • 1
    @pkamb, the comments in that answer did not work for me. The database is private and they're talking about public databases. – Miles Oct 08 '21 at 22:21

1 Answers1

0

Depending on when you started using NSPersistentCloudKitContainer the problems you're seeing may well be related to a bug I think I found in iOS 15 in an early beta which persists in the publicly released versions of iOS 15 - assuming the problem is only being seen by users running iOS 15?

Apple has acknowledged that there is an issue here and they're investigating it at present.

See here: NSPersistentCloudkitContainer Memory Leak -> Crash? (iOS 15 beta 4, 5, 6 & 7...)

Paul Martin
  • 699
  • 4
  • 13
  • The problem actually precedes iOS 15. Thanks for the link though – Miles Oct 12 '21 at 16:40
  • Hmm... that's interesting, and confusing considering *some* users see data sync correctly and others don't. When I started using NSPersistentCloudKitContainer I would notice occasional sync fails like this ages ago. After going through my Core Data model and comparing it to the iCloud fields, I found the culprit - a single (unused!) Core Data attribute was missing on the iCloud side. It prevented a specific object from syncing (which prevented everything else from syncing). Might be that? – Paul Martin Oct 12 '21 at 19:08