SwiftUI macOS app not syncing with iCloud

I have an iOS and iPadOS app that use iCloud to sync. They are in the App Store and the database was pushed to production. They work as advertised. I wrote also a macOS app that uses Cloudkit, the container it set as NSPersistentCloudKitContainer and the merge policies are set ok. I also checked that the profile for the app and certificates are good to go, as well as the permissions of the app.

Apple signed the app, but, it doesn't sync with iCloud and I can't figure out why. I checked iCloud on my Mac and the app is there, set to be syncing. I have already read a few articles and have tried all the possible solutions, including signing out and back in, rebuilding the app, etc.

One thing, thought, when I delete an item, the app takes sometime to react to it, as if it's trying to connect. I tried with TCPDump, and other things to check for connection issues, but I can't find that app trying to connect.

I took a few screenshots of the different configs, etc so you can see as well, but has anyone run into this issue? Do you know a possible solution?

Thank you.

Screenshots:

Code in persistance.swift:

import CoreData

struct PersistenceController {
    static let shared = PersistenceController()

    static var preview: PersistenceController = {
        let result = PersistenceController(inMemory: true)
        let viewContext = result.container.viewContext
        for _ in 0..<10 {
            let newBolt = Jot(context: viewContext)
            newBolt.id = UUID()
            newBolt.text = "random"
        }
        do {
            try viewContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
        return result
    }()

    let container: NSPersistentCloudKitContainer

    init(inMemory: Bool = false) {
        container = NSPersistentCloudKitContainer(name: "Simple_Bolt")
        
        let description = container.persistentStoreDescriptions.first
        description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        description?.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

        
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
        container.viewContext.automaticallyMergesChangesFromParent = true
    }
}
Answered by savageoldman in 735892022

Well, the answer was simple and yet hard to find. It seems when you convert a Core Data app to work with CloudKit you need to add CloudKit.framework to the Frameworks, Libraries, and Embedded Content section. without it, the app will work when running in debug mode (via Xcode) but once it's in production, signed and notarized, it won't. Even if you have the right entitlements, all pointing to production, etc.

So, I found a few answers in https://medium.com/macos-app-development/using-cloudkit-on-macos-app-2647cb0f91d6

I added com.apple.developer.icloud-container-environment to the entitlements and set to production and now when I run the app in debug mode, it syncs with my production container. However, when I export and sign the app (via Xcode Archive -> Distribute App -> Developer ID) then again it doesn't sync.

I'm trying to figure out what else am I missing? There seem to be no documentation anywhere.

Accepted Answer

Well, the answer was simple and yet hard to find. It seems when you convert a Core Data app to work with CloudKit you need to add CloudKit.framework to the Frameworks, Libraries, and Embedded Content section. without it, the app will work when running in debug mode (via Xcode) but once it's in production, signed and notarized, it won't. Even if you have the right entitlements, all pointing to production, etc.

SwiftUI macOS app not syncing with iCloud
 
 
Q