与 CloudKit 共享核心数据:无法接受共享

Core Data sharing with CloudKit: Unable to Accept Share

我让我的应用程序使用 Core Data,然后 CloudKit 在设备之间同步,现在我想在用户之间共享数据。我看了两个 Build apps that share data through CloudKit and Core Data and What's new in CloudKit WWDC21 并认为我把概念记下来了。 CloudKit 使用区域共享和 CKShares 来处理共享,Core Data 在 iOS15.

中原生地附加到这个实现

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

/// 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 中使用 UICloudSharingController 共享 NSObject 后,如下所示:

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 调试器(相同版本和构建)启动的。此外,我无法在其他问题上找到此警报的参考,因此欢迎任何建议、建议或帮助,因为我已经坚持了几个晚上。如果有更多信息可以阐明这个问题,请告诉我。

我有同样的问题,当我在 Info.plist

之后我就可以毫无问题地分享了。