Initialising CloudKit schema on public database fails for in Core Data with multiple strings (rdar://FB8995024)

Hi all,

I'm trying to use NSPersistentCloudKitContainer.initializeCloudKitSchema() to initialise a public CloudKit database, but it seems to fail when any given entity has more than two String fields (or, it seems, any two fields which NSPersistentCloudKitContainer decides need to be CKAsset-backed).

I've raised rdar://FB8995024 about this too, but was wondering if someone here can see something I'm missing.

I can consistently reproduce this on iOS 14.4 (18D46) on the iOS Simulator with Xcode 12.4 (12D4e), or with an iPhone 11 Pro running iOS 14.4 (18D52) using this code (in a new iOS App project, SwiftUI/SwiftUI App/Swift, Use Core Data+Host in CloudKit) and a single Core Data entity with two String attributes:

Code Block swift
import CoreData
import SwiftUI
@main
struct NSPersistentCloudKitContainer_initTestingApp: App {
    let persistenceController = PersistenceController.shared
    var body: some Scene {
        WindowGroup {
            Text("Success")
        }
    }
}
struct PersistenceController {
    static let shared = PersistenceController()
    let container: NSPersistentCloudKitContainer
    init() {
        container = NSPersistentCloudKitContainer(name: "NSPersistentCloudKitContainer_initTesting")
// If we change this to .private, everything is fine
        container.persistentStoreDescriptions[0].cloudKitContainerOptions?.databaseScope = .public
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        do {
            try container.initializeCloudKitSchema()
        } catch {
            fatalError("###\(#function) Error initialising CloudKit schema: \(String(describing: error))")
        }
    }
}


Thanks!

This is still reproducible on iOS 15.0b1 simulators (19A5261u) in Xcode 13.0 beta (13A5154h), with the same code. I've attached the console log I'm seeing on those builds to the aforementioned feedback.

You should be able to work around this by initializing your schema with a private-database-scoped-store.

@Nick: Initialising against the private database as you suggested works on iOS 14.5. It would be really useful if that was in the documentation for initializeCloudKitSchema() if it's not likely to be fixed to support initialising against a public database, to avoid others falling into the trap I did! I've commented to that effect on FB8995024, too.

On iOS 15, I can't verify if that's also the case yet because of FB9156476 (the feedback I created during our lab session). For the sake of anyone else who finds this thread, the error that feedback refers to is below.


FB9156476 (iOS 15 NSPersistentCloudKitContainer can't sync initial data from server with “Failed to sync user keys”) error:

2021-06-10 21:51:53.672655+0100 MyApp[1230:15921] [error] error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(955): <NSCloudKitMirroringDelegate: 0x60000131c180>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x7fa1b1e05f50> (URL: file:///Users/steve/Library/Developer/CoreSimulator/Devices/59435252-2CF1-4980-8ABB-4B46B21385AA/data/Containers/Data/Application/CC6176E7-4FDC-4842-A68F-C6198170CAEA/Library/Application%20Support/private.sqlite)
<CKError 0x600002881c50: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
	com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600002805470: "Internal Error" (1/5000); "Failed to sync user keys">
}>

Meet same issue and find a solution:

Don't use initializeCloudKitSchema(),

instead of creating an entity, then the system will auto generate schema, without creating extra field( CD_***_ckasset).

Example:

You use 「PublicEntity」 in 「Public」Configuration;

  • Create a 「PublicEntity」
  • set value for every field
  • use context.save()

then schema 「CD_PublicEntity」 will auto generate.

Initialising CloudKit schema on public database fails for in Core Data with multiple strings (rdar://FB8995024)
 
 
Q