Can the NSPersistentCloudKitContainer mirror the data from the cloudKit public database to the local Core Data if the user is not logged in?

I'm currently syncing Core Data with the CloudKit public database using NSPersistentCloudKitContainer. The app starts with an empty Core Data store locally and at the app launch it downloads the data from CloudKit public database to the Core Data store, but this can only be accomplished if the user is logged in, if the user is not logged, no data gets downloaded and I get the error below.

Can the NSPersistentCloudKitContainer mirror the data from the CloudKit public database to the local Core Data even if the user is not logged in? Can someone please confirm this is possible?

The reason for my question is because I was under the impression that the users didn't need to be logged to read data from the public database in CloudKit but I'm not sure this applies to NSPersistentCloudKitContainer when mirroring data. I know I can fetch data directly with CloudKit APIs without the user beign logged but I need to understand if NSPersistentCloudKitContainer should in theory work without the user being logged.

I hope someone from Apple sees this question since I have spent too much time researching without any luck.

Error

Error fetching user record ID: <CKError 0x600000cb1b00: "Not Authenticated" (9); "No iCloud account is configured">

CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1192): <NSCloudKitMirroringDelegate: 0x600003b00460>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x10700f0d0> (URL: file:///Users/UserName/...

That's worth a feedback report with the full logs. NSPersistentCloudKitContainer is supposed to be compatible with account-less public database use.

That's worth a feedback report with a sysdiagnose or the logs from your test run.

Here is the code that works only if the user is logged in. Again, I'm connecting to the Public database.

    class CoreDataManager: ObservableObject{
        // Singleton
        static let instance = CoreDataManager()
        private let queue = DispatchQueue(label: "CoreDataManagerQueue")
        
        private var iCloudSync = true
        
        lazy var context: NSManagedObjectContext = {
        return container.viewContext
        }()
        
        lazy var container: NSPersistentContainer = {
        return setupContainer()
        }()
            
        func updateCloudKitContainer() {
            queue.sync {
                container = setupContainer()
            }
        }
        
        func setupContainer()->NSPersistentContainer{
            let container = NSPersistentCloudKitContainer(name: "CoreDataContainer")
            
            guard let description = container.persistentStoreDescriptions.first else{
                fatalError("###\(#function): Failed to retrieve a persistent store description.")
            }
            
            description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        
            let cloudKitContainerIdentifier = "iCloud.com.example.PublicDatabaseTest"
                
            let options = NSPersistentCloudKitContainerOptions(containerIdentifier: cloudKitContainerIdentifier)
            description.cloudKitContainerOptions = options
                
            description.cloudKitContainerOptions?.databaseScope = .public // Specify Public Database
            
            container.loadPersistentStores { (description, error) in
                if let error = error{
                    print("Error loading Core Data. \(error)")
                }
            }
            container.viewContext.automaticallyMergesChangesFromParent = true
            container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

            return container
        }
        
        func save(){
            do{
                try context.save()
            }catch let error{
                print("Error saving Core Data. \(error.localizedDescription)")
            }
        }
    }

Just to clarify, the error shown in my original post is not an error that makes the app crash, it's an error I get in the debug console, I assume it's an error logged by CloudKit.

Can the NSPersistentCloudKitContainer mirror the data from the cloudKit public database to the local Core Data if the user is not logged in?
 
 
Q