0

Has anyone been able to get the iOS 14 Beta to work and save/share records using the Public Database?

I have yet to get this to work with Public. Using their code, I can create new records, and see them in the CloudKit Dashboard, but I only see the Private records. When I add the code for the public DB, I cannot see those records when creating other than locally. (Meaning, I cannot see them in the Dashboard) Is there something else special I would need to do to save them in the public DB and then see them in the Dashboard? I do see that when I create the records and they show in the Private DB, they are in a Zone named: com.apple.coredata.cloudkit.zone. I can manually add records in the Public DB via Dashboard, but do not see them when I run app on phone. Any suggestions are appreciated. Thx

        
        // Create a container that can load CloudKit-backed stores
        let container = NSPersistentCloudKitContainer(name: "CoreDataCloudKitDemo")
        
        // Enable history tracking and remote notifications
        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("###\(#function): Failed to retrieve a persistent store description.")
        }

        description.cloudKitContainerOptions?.databaseScope = .public
        
        description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

        container.loadPersistentStores(completionHandler: { (_, error) in
            guard let error = error as NSError? else { return }
            fatalError("###\(#function): Failed to load persistent stores:\(error)")
        })
        
        container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        container.viewContext.transactionAuthor = appTransactionAuthorName
        
        // Pin the viewContext to the current generation token and set it to keep itself up to date with local changes.
        container.viewContext.automaticallyMergesChangesFromParent = true
        do {
            try container.viewContext.setQueryGenerationFrom(.current)
        } catch {
            fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")
        }
        
        // Observe Core Data remote change notifications.
        NotificationCenter.default.addObserver(
            self, selector: #selector(type(of: self).storeRemoteChange(_:)),
            name: .NSPersistentStoreRemoteChange, object: container)
        
        return container
    }()
George
  • 514
  • 1
  • 8
  • 26

1 Answers1

0

I figured it out. I forgot to add the two indexes to the database for using Public. Needed to add recordName and ModifiedAt to schema on all recordTypes.

George
  • 514
  • 1
  • 8
  • 26