CloudKit - Problems with template application

G'day everyone,

I've had some problems with CoreData+CloudKit, and figured I would go back to basics and start from scratch with a new project. (Note: I'm using the Xcode 13.0 Beta with iOS 15 to try to use sharing on NSPersistentCloudKitContainer as discussed in WWDC2021, but these problems are scattered over the net without any answers to date)

When I go through the motions of adding iCloud to the project, assign a new (unique) container and then build the app, two problems arise:

  1. the simulator comes back with a blank screen (without the Edit or add controls from the boilerplate). Even the preview pane seems broken (no "Add Item" button). This worked in Xcode 12, so I am assuming there is something that's come up as a bug here.

  2. More importantly, I get a swath of errors and warnings in the debug console related to CloudKit. It looks like there is some sort of identity problem here. There is no schema generated as yet in the CloudKit dashboard, so I am guessing that the migration errors are happening because the database hasn't yet established because of the identify problem.

Does anyone have any insight into what I'm missing here?

Thanks.

OK. One of the problems was that the body method of the ContentView isn't correct out of the box... once resolved it performs correctly in Xcode 12 under the accompanying simulator.

However in the current Xcode 13 Beta it fails to operate as expected with CloudKit (I've reported this via Feedback), resulting in the identify issues as above ("Failed to modify some record zones", "Failed to set up CloudKit integration for store", "Failed to modify some record zones", "Failed to sync user keys" are the errors being reported)

I believe the ContentView.swift body method should read as follows:


        NavigationView {
            List {
                ForEach(items) { item in
                    Text("Item at \(item.timestamp!, formatter: itemFormatter)")
                }
                .onDelete(perform: deleteItems)
            }

            .toolbar {
                #if os(iOS)
                ToolbarItem(placement: .bottomBar){
                    EditButton()
                }
                #endif

                ToolbarItem (placement: .primaryAction) {
                    Button(action: addItem) {
                        Label("Add an Item", systemImage: "plus")
                    }
                }
            }
        }
    

Andy, I'm having the same issues. Did you ever get to the bottom of this one? The errors look like this: Failed to sync user keys Failed to set up CloudKit integration for store: <NSSQLCore:

OK. More experimenting and the plot thickens.

To check whether this was a simulator or Xcode issue, I deployed the modified base application to a local iPad and iPhone. Interestingly the user sync errors now go away, maying me believe there is something wrong with the sign-in capability on the Beta simulator...

However no record changes were observable in the CloudKit Dashboard. I tried signing out and in again without any change.

I then reset the dev environment in CloudKit, and restarted the apps on the devices.

At this stage, interestingly, they show some interesting behaviour.

  1. The items previously created are gone - that makes sense, since I just blew away the development data
  2. The Schema is not loaded, as expected for a refreshed Dev environment (i.e. the CD_Item record type doesn't exist)
  3. When I add my first item in the app, the CD_Item record type appears in the schema
  4. I then add an index on RecordID, and query the database, which yields no records.

So the app is definitely communicating successfully with CloudKit now, as it's building the schema, but the data is not appearing in the database.

And it gets more interesting again.

When I add an item on the iPhone, the iPad debug window seems to indicate it is getting push notifications from the database update, but those changes don't appear in the iPad interface...

And lastly, if I then try to delete an Item on the iPad, I get an error message that a merge conflict could not be resolved.

Has something become pretty seriously unravelled in this latest release?

G'day Stokaace,

Nope - still waiting for anyone from engineering to see these apparently. I might try to hit up Nick directly. Seems like a pretty fundamental issue if the basic boilerplate app won't run successfully...

I've had a look at the demo code Nick put up, but it's "Old School" UI, rather than SwiftUI, which is a bit frustrating given the high recommendations to move development towards this "new" framework.

Interestingly still no fix in Beta 2, and not a lot of conversation. Am I the only one encountering this (I know that I'm not, but it does seem very quiet)

Beta 3 seems to have resolved most of the issues, with Beta 4 fixing the problems with provisioning profiles... back to trying to get sharing to work...

If anyone has seen cktool in action please let me know - I can't find it anywhere in the beta installations :)

Not sure if this helps, but while playing with SwiftUI I have recently created a project including CoreData + CloudKit using their template and what I saw are some warnings related to the stores not be loaded, to fix them I did two things:

  • I set some of the properties after loading the stores (I'm looking to move this into the completion block as it makes more sense to me)
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in

            if let error = error as NSError? {

                // Replace this implementation with code to handle the error appropriately.

                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                

                /*

                 Typical reasons for an error here include:

                 * The parent directory does not exist, cannot be created, or disallows writing.

                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.

                 * The device is out of space.

                 * The store could not be migrated to the current model version.

                 Check the error message to determine what the actual problem was.

                 */

                fatalError("Unresolved error \(error), \(error.userInfo)")

            }

        })

        if inMemory {

            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")

        }

        container.viewContext.automaticallyMergesChangesFromParent = true
  • Be sure the simulator I'm using has a valid iCloud account setup.
CloudKit - Problems with template application
 
 
Q