Share Core Data between users with NSPersistentCloudKitContainer

给你一囗甜甜゛ 提交于 2020-07-18 05:40:30

问题


Apple introduced the NSPersistentCloudKitContainer with iOS13 which enable us to use CloudKit with 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.


回答1:


There are methods on NSPersistentCloudKitContainer for accessing the underlying cloudkit records: 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.

BUT as of the current beta release (beta 3) these methods seem to return 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.




回答2:


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

The possible values are .public (the public database), .private (the private database) and .shared (the shared database).

E.g.:

let container = NSPersistentCloudKitContainer(name: "test")
guard let description = container.persistentStoreDescription.first else {
fatalError("Error")
}
description.cloudKitContainerOptions?.databaseScrope = .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://stackoverflow.com/questions/56768719/share-core-data-between-users-with-nspersistentcloudkitcontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!