1

Is it possible?

I've got a new iOS 14 app set up with Core Data and Cloudkit. I had to make a few changes to my Persistence.swift file to get it working but it's working without a hitch.

I'm interested in implementing sharing with other iCloud Users, but a lot of the documentation is out of date and confusing and it seems like it might not have been possible at one point but it is now?

I think the first step is to make my database shared? I'm adding the following line to my Persistence.swift file

container.persistentStoreDescriptions.first!.cloudKitContainerOptions?.databaseScope = .shared

(Here's the whole thing):

import CoreData

struct PersistenceController {
    static let shared = PersistenceController()
    let container: NSPersistentCloudKitContainer

    init(inMemory: Bool = false) {
        container = NSPersistentCloudKitContainer(name: "Shopmatic")

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })

        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        
        container.persistentStoreDescriptions.first!.cloudKitContainerOptions?.databaseScope = .shared
    }
}

but when I run the app I get the following error

Thread 1: "CKDatabaseScopeShared is not supported with NSPersistentCloudKitContainer"

Which is not encouraging, but maybe it's possible to implement sharing with a public database scope?

I'm not really sure what the next steps are? Implementing my own NSPersistentContainer that both syncs to cloudkit and allows sharing?

5
  • 1
    You can with AppGroup and by sharing the cloud kit container Commented Mar 24, 2021 at 1:53
  • @loremipsum interesting, first time I'm hearing of AppGroup but I will investigate further. Please feel free to elaborate into a full answer if you like. Commented Mar 24, 2021 at 2:14
  • I’ll post some code later (I’m away from my Mac right now) ran into this trying to get CoreData to work with a Widget and other user devices. It isn’t perfect because I can’t get the widget to update when the CoreData object is edited but the widget selections update simultaneously. Commented Mar 24, 2021 at 11:32
  • 1
    Do you want to share the database with an extension in the same device (i.e. Widgets)? Or do you want to share your objects with another person? Commented Mar 24, 2021 at 12:57
  • @EmilioPelaez oh yes, I want to implement sharing with other people, i.e: developer.apple.com/documentation/cloudkit/shared_records/… Commented Mar 24, 2021 at 17:59

2 Answers 2

1

The Apple engineer's answer in the forum reads quite clearly to me:

I have not implemented such a feature yet, but I would approach it like this:

  1. Get the NSPersistantCloudKitContainer implementation working.
  2. Implement CloudKit sharing on the same container.
  3. Start passing specific records from NSPersistantCloudKitContainer into the sharing feature implemented in step 2.

To access the shared Cloudkit database with CKDatabase.Scope = shared:

If this is the NSPersistentCloudKitContainer
:

container = NSPersistentCloudKitContainer(name: "AppName")


I would try to access the shared DB of this container like so
:

let myAppsCloudKitContainer = CKContainer(identifier: "iCloud.com.name.AppName")
let myContainersSharedDatabase = myAppsCloudKitContainer.database(with: .shared)

To access data from NSPersistantCloudKitContainer for use in the CloudKit sharing feature use NSPersistantCloudKitContainer instance method

record(for managedObjectID: NSManagedObjectID)

(as of macOS 11 / Xcode 12)

Sign up to request clarification or add additional context in comments.

1 Comment

it actually does support sharing: developer.apple.com/videos/play/wwdc2021/10015 though I dont think anyone has got it working yet. The doc in typical apple coredata/cloudkit fashion was out of date even when they presented it at WWDC 2021 (it still uses AppDelegate for example) and doesn't seem to work.
1

EDIT: As of May 2023 the answer now appears to be yes. There are docs and a sample app located here:

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

The sample project requires a minimum of iOS 16.4 and macOS 13.3.

OLD ANSWER:

I believe the answer is no based on this thread on the Apple dev forums (I'm MasonAndMuse there), but there's one Apple engineer in there who was saying yes but wouldn't elaborate. I stopped trying soon after, not finding any way around the opaque nature of the system as it is now. Nothing I've seen indicates someone else has gotten it working but I haven't looked very closely after giving up 8 months ago. Of course there might be a way - I'd love to be proven wrong!

I assume they introduce sharing in iOS 15, but that's not very helpful now... Too bad because it's so close now and it will save SO much time if/when they add it in...

1 Comment

Oh yes, came across that thread myself, kind of shocking that Apple staff is joking about it! I'll keep digging and keep this page updated with any findings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.