首页 > 解决方案 > 将 cloudkit 与 coredata 一起使用时,模拟器和真实设备永远不会相互同步

问题描述

我使用核心数据作为本地存储,使用 Cloudkit 作为后端来托管我的 iOS 应用程序。我使用 NSPersistentCloudKitContainer 与 Coredata + Cloudkit 一起工作。

这就是代码的样子

struct UserProfile {

 static let shared = UserProfile.init()
 
 let container: NSPersistentCloudKitContainer
    
    init() {
        container = NSPersistentCloudKitContainer.init(name: "Profile")
        
        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("Unable to retrieve a persistent store description")
        }
        
        
        description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        
        
        container.loadPersistentStores { storeDescription, error in
            if let error = error as NSError? {
                fatalError("Unable to load todo:\(error)")
            }
        }
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

        
    }

}

在保存数据之前,我确保模拟器有一个 icloud 帐户。到目前为止,我已经测试过数据已与具有相同帐户的多个模拟器成功同步。

但是当我用真实设备测试模拟器时,反之亦然。它不会相互同步,就好像两者都被视为不同的设备一样。(两者拥有相同的帐户)

我确保一切都已相应设置:

任何帮助表示赞赏


更新:比较是通过模拟器上的应用程序和从 testflight 下载的应用程序进行的。两者都有相同的 icloud 帐户。

标签: swiftcore-dataicloudcloudkitnspersistentcloudkitcontainer

解决方案


推荐阅读