Reputation: 43
I'm trying to store some data in Cloudkit by Xcode but when I push QueryRecords in the Dashboard "There are no "Entity" records in this database" is displayed but my "SaveFunction" reports a "Saved successfully". Did I miss something ? I'm very stuck.... Thank you for any help you can offer!...
1) Signing & Capabilities Part :
2) Xcdatamodeld
3) AppDelegate Part
[...]
lazy var persistentContainer: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "NameOfContainer")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
4) ViewController
class ViewController: UIViewController {
let myContainer = CKContainer(identifier: "iCloud.com.MyUserName.NameOfTheProject")
override func viewDidLoad() {
super.viewDidLoad()
let artworkRecord = CKRecord(recordType: "Entity")
artworkRecord["customFieldName"] = "xxxx" as NSString
// Do any additional setup after loading the view.
let privateDatabase = myContainer.privateCloudDatabase
privateDatabase.save(artworkRecord) { (record, error) in
if let error = error {
print(error)
// Insert error handling
return
}
print("Saved successfully")
// Insert successfully saved record code
}
}
}
5) CloudKit Dashboard
Upvotes: 3
Views: 690
Reputation: 103
I had the same issue. Turns out CoreData doesn't use the zone _defaultZone
for private databases. Instead, it created one called com.apple.coredata.cloudkit.zone
.
After switching the filter to the correct zone I can query all records created using my own user.
I couldn't find any documentation about this, but will update the answer if I discover more about it.
Upvotes: 3
Reputation: 1943
Please, check if your development account and your iCloud account on your test device are the same. I also have the problem, when I test iCloud access on a test device, that has a different apple-id then my development apple-id.
Like the Apple documentation in the section "Select or Create an iCloud Account for Development" says: "Note that your iCloud account is distinct from your Apple Developer account; however, you can use the same email address for both. Doing so gives you access to your iCloud account’s private user data in CloudKit Dashboard, which can be helpful for debugging."
You are using the private database and if you then want to see the private records, your test device must have the same apple-id as your development account id.
Kind regards, MacUserT
Upvotes: 3