Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronizing Core Data to CloudKit Public Database

In the following link, there is the source code for a project that allows you to sync core data between the same user devices. Data is stored in User Private Database... is there a way to sync data from CoreData in public CloudKit database so the app will be the same between ALL app users? Why NSPersistentCloudKitContainer doesn't allow you to set this?

https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud

like image 331
Lorenzo Fiamingo Avatar asked May 11 '26 07:05

Lorenzo Fiamingo


1 Answers

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.persistentStoreDescriptions.first else {
    fatalError("Error")
}
description.cloudKitContainerOptions?.databaseScope = .public

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.

You may also need an #available check to ensure backwards compatibility, ie:

if #available(iOS 14.0, *) {
    description.cloudKitContainerOptions?.databaseScope = .public
} else {
    // Fallback on earlier versions
}
like image 177
Bas Avatar answered May 12 '26 23:05

Bas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!