1

我的应用程序使用 Core Data,然后使用 CloudKit 在设备之间同步,现在我想在用户之间共享数据。我观看了通过 CloudKit 和 Core Data 共享数据的构建应用程序以及CloudKit WWDC21 中的新功能,并认为我已经掌握了这些概念。CloudKit 在 iOS15 中使用区域共享和 CKShares 来处理共享和核心数据附加到这个实现。

我这样设置我的核心数据堆栈:

/// Configure private store
guard let privateStoreDescription: NSPersistentStoreDescription = persistentContainer.persistentStoreDescriptions.first else {
    Logger.model.error("Unable to get private Core Data persistent store description")
    return
}
privateStoreDescription.url = inMemory ? URL(fileURLWithPath: "/dev/null") : privateStoreDescription.url?.appendingPathComponent("\(containerIdentifier).private.sqlite")
privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
persistentContainer.persistentStoreDescriptions.append(privateStoreDescription)

/// Create shared store
let sharedStoreDescription: NSPersistentStoreDescription = privateStoreDescription.copy() as! NSPersistentStoreDescription
sharedStoreDescription.url = sharedStoreDescription.url?.appendingPathComponent("\(containerIdentifier).shared.sqlite")
let sharedStoreOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier)
sharedStoreOptions.databaseScope = .shared
sharedStoreDescription.cloudKitContainerOptions = sharedStoreOptions
persistentContainer.persistentStoreDescriptions.append(sharedStoreDescription)
persistentContainer.loadPersistentStores(...)

实现了 SceneDelegate 用户接受:

func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
    let container = PersistenceController.shared.persistentContainer
    let sharedStore = container.persistentStoreCoordinator.persistentStores.first!
    container.acceptShareInvitations(from: [cloudKitShareMetadata], into: sharedStore, completion: nil) //TODO: Log completion
}

但是,在我的 UI 中共享 NSObject 后,使用UICloudSharingController如下所示:

let object: NSObject = // Get Object from view context
let container = PersistenceController.shared.persistentContainer
let cloudSharingController = UICloudSharingController { (controller, completion: @escaping (CKShare?, CKContainer?, Error?) -> Void) in
    container.share([object], to: nil) { objectIDs, share, container, error in
        completion(share, container, error)
        Logger.viewModel.debug("Shared \(household.getName())")
    }
}
cloudSharingController.delegate = self
self.present(cloudSharingController, animated: true) {}

我的 SceneDelegate 方法永远不会被调用,当我从消息应用程序中按下邀请时,我会收到以下警报。我不太确定在这种情况下出了什么问题,因为在 CloudKit 开发人员控制台上,我在私有数据库中看到该对象,其区域为 com.apple.coredata.cloudkit.share.[UUID]。我还没有发布这个应用程序,所以我不确定它从哪里获取版本信息,因为这两个应用程序都是从 Xcode 调试器(相同的版本和构建)启动的。此外,我无法在其他问题上找到此警报的参考,因此欢迎任何建议、建议或帮助,因为我已经坚持了几个晚上。如果有更多信息可以阐明这个问题,请告诉我。

4

1 回答 1

2

我遇到了同样的问题,当我在 Info.plist 中添加 Bool 值为 true 的 CKSharingSupported 键时,它得到了解决

之后,我能够毫无问题地分享。

于 2022-01-23T02:15:48.230 回答