简体   繁体   English

使用 NSPersistentCloudKitContainer 在用户之间共享核心数据

[英]Share Core Data between users with NSPersistentCloudKitContainer

Apple introduced the NSPersistentCloudKitContainer with iOS 13 which enable us to use CloudKit with Core Data. Apple 在 iOS 13 中引入了NSPersistentCloudKitContainer ,它使我们能够将 CloudKit 与 Core Data 一起使用。 I got it working pretty much instantly on different devices but my main issue is still left.我让它在不同的设备上几乎立即工作,但我的主要问题仍然存在。

Is it possible to share the data in an easy way with other users?是否可以以简单的方式与其他用户共享数据? I've been reading on CKShare but don't see how I can go from NSPersistentCloudKitContainer to that in an easy way.我一直在阅读CKShare但不知道如何以简单的方式从NSPersistentCloudKitContainer转到那个。

It seems this is now possible in iOS 14.0+ Beta and macOS 11.0+ Beta via the new databaseScope property: https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontaineroptions/3580372-databasescope现在似乎可以通过新的databaseScope属性在 iOS 14.0+ Beta 和 macOS 11.0+ Beta 中实现这一点: https : //developer.apple.com/documentation/coredata/nspersistentcloudkitcontaineroptions/3580372-databasescope

The possible values are .public (the public database), .private (the private database) and .shared (the shared database).可能的值为.public (公共数据库) 、.private (私有数据库)和.shared (共享数据库)。

Eg:例如:

let container = NSPersistentCloudKitContainer(name: "test")
guard let description = container.persistentStoreDescription.first else {
fatalError("Error")
}
description.cloudKitContainerOptions?.databaseScope = .shared

The video https://developer.apple.com/videos/play/wwdc2020/10650 describes how to sync the Core Data store with the CloudKit public database by setting the databaseScope value to .public .视频https://developer.apple.com/videos/play/wwdc2020/10650描述了如何通过将databaseScope值设置为.public来同步 Core Data 存储与 CloudKit 公共数据库。

[ UPDATE ] Unfortunately, it seems sharing is not (yet?) supported, even though the databaseScope = .shared property suggests otherwise. [更新] 不幸的是,似乎(还?)不支持共享,即使databaseScope = .shared属性表明并非如此。 Please refer to https://developer.apple.com/forums/thread/649630?login=true&page=1#621748022 as pointed out by Brian M below.请参阅https://developer.apple.com/forums/thread/649630?login=true&page=1#621748022,如下面的 Brian M 所指出。

As announced in WWDC 2021, you can now share to other iCloud users.正如 WWDC 2021 中宣布的那样,您现在可以与其他 iCloud 用户共享。 https://developer.apple.com/videos/play/wwdc2021/10015/ https://developer.apple.com/videos/play/wwdc2021/10015/

There are methods on NSPersistentCloudKitContainer for accessing the underlying cloudkit records: https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer . NSPersistentCloudKitContainer 上有一些用于访问底层 cloudkit 记录的方法: https : //developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer For example,例如,

func record(for managedObjectID: NSManagedObjectID) -> CKRecord?

So in theory you could use this method to obtain a CKRecord then create a CKShare manually.所以理论上你可以使用这个方法来获取一个 CKRecord,然后手动创建一个 CKShare。

BUT as of the current beta release (beta 3) these methods seem to return nil.但是从当前的 beta 版本(beta 3)开始,这些方法似乎返回 nil。 It seems like they wouldn't have included these methods if they wanted to keep the implementation hidden.如果他们想隐藏实现,他们似乎不会包含这些方法。 So we're in this spot where you can implement the entire sync yourself and get sharing, or use their sync implementation but not get sharing.所以我们在这个地方你可以自己实现整个同步并获得共享,或者使用他们的同步实现但不共享。 I hope the lack of implementation on these methods is simply an early beta issue.我希望这些方法缺乏实现只是一个早期的测试版问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM