I have a working app with core data and cloudkit. I changed the NSPersistentContainer to NSPersistentCloudKitContainer. Two NSManagedObjects 'Contact' and 'Location' already exist in core data with relationships along with some other related NSManagedObjects. Populated core data in 'Contact' and saved. Logged in to CloudKit to see it had created CD_Contact and CD_Location CKRecords along with some other CKRecords. Queried CD_Contact it gave error - index name not available on recordName. So created an Index of type Queryable on recordName and searched again. There is no data in CD_Contact. It is supposed to sync data from core data to cloud Kit, but only CKRecord is created but data saved in core data is not synced and populated in CloudKit. How to fix this? On debug I get this:
"<CKRecord: 0x104f0afc0; recordType=CD_Contact, recordID=558FD3FB-A293-444C-8894-A1A7B786D73A:(com.apple.coredata.cloudkit.zone:__defaultOwner__), recordChangeTag=15, values={\n \"CD_assignedEEP\" = 0;\n \"CD_createdDate\" = \"2022-11-03 04:14:17 +0000\";\n \"CD_email\" = \"crsp@gmail\";\n \"CD_entityName\" = Contact;\n \"CD_firstName\" = chaya;\n \"CD_imageName\" = \"{ length=87143, sha256=ed66d0026ea41aed08e213928643398504dea984c010b80a8852a0fb72c80350 }\";\n \"CD_lastName\" = rao;\n \"CD_me\" = 0;\n \"CD_orgName\" = \"\";\n \"CD_phone\" = 8327292039;\n \"CD_screenName\" = cvr;\n \"CD_uniqueId\" = \"A2469E8D-782F-49F5-8186-3C38B8F9B715-55813-00001051FEDEEF2F\";\n \"CD_userRating\" = \"Not rated\";\n \"CD_userType\" = \"Consumer & Producer(Household)\";\n}>",
"CD_contactEmail" = "crsp@gmail";
"CD_contactEmail_ckAsset",
"CD_contact",
"CD_contactEmail",
"<CKRecord: 0x111659350; recordType=CD_Location, recordID=D6FA5778-587E-4778-925E-197AD5ABC66B:(com.apple.coredata.cloudkit.zone:__defaultOwner__), recordChangeTag=19, values={\n \"CD_apt\" = \"\";\n \"CD_assignedEEP\" = 0;\n \"CD_batterykWh\" = 0;\n \"CD_city\" = Houston;\n \"CD_contactEmail\" = \"crsp@gmail\";\n \"CD_country\" = US;\n \"CD_entityName\" = Location;\n \"CD_evkWh\" = 0;\n \"CD_latitude\" = \"29.92885756496788\";\n \"CD_loadWatts\" = 0;\n \"CD_loadkWh\" = 0;\n \"CD_locationName\" = myloc;\n \"CD_locationType\" = Household;\n \"CD_longitude\" = \"-95.60777006842892\";\n \"CD_netMeterEsid\" = 1008901023808105650100;\n \"CD_provider\" = \"AMIGO ENERGY\";\n \"CD_providerType\" = \"Energy Provider\";\n \"CD_solarkWh\" = 0;\n \"CD_state\" = TX;\n \"CD_street\" = \"11202 Hillside Glen Trail \";\n \"CD_uniqueId\" = \"27D61490-B38A-4F0A-A5EC-76BDBDEBA46F-55813-00001052B902170C\";\n \"CD_windkWh\" = 0;\n \"CD_zip\" = 77065;\n}>"
My AppDelegate Code:
let container = NSPersistentCloudKitContainer(name: "MyModel")
let defaultStoreURL = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("MyModel.xcdatamodeld")
let description = NSPersistentStoreDescription(url: defaultStoreURL)
let cloudkitOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.ggp.nr")
description.cloudKitContainerOptions = cloudkitOptions
let remoteChangeKey = "NSPersistentStoreRemoteChangeNotificationOptionKey"
description.setOption(true as NSNumber,
forKey: remoteChangeKey)
description.setOption(true as NSNumber,
forKey: NSPersistentHistoryTrackingKey)
container.persistentStoreDescriptions = [description]
container.viewContext.automaticallyMergesChangesFromParent = true
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
// if FileManager.default.fileExists(atPath: self.storeURL.path) {
// self.duplicateRecordsAndDeleteOldStore(newContext: container.newBackgroundContext(), mom: container.managedObjectModel)
// }
})
return container
}()
Also in my cloudKit database "iCloud.com.ggp.nr", under security roles for iCloud and World I have set Read and Write permissions for all the CD_ Records. Don't know what else to do. Any help is appreciated.