Hello everyone, I'm working on an iOS app that uses CloudKit for data synchronization. I'm encountering an issue where my app can't find the "JournalPrompt" record type in the public database. Here's the relevant code and error messages (I'm using placeholders like [APP_NAME] or [CONTAINER_IDENTIFIER]):
private func fetchPromptsFromiCloud() {
let container = CKContainer(identifier: "[CONTAINER_IDENTIFIER]")
let publicDatabase = container.publicCloudDatabase
// Create a predicate to query for the specific record
let predicate = NSPredicate(format: "recordID.recordName == %@", "B6663053-FC2E-4645-938B-9FA528D59663")
let query = CKQuery(recordType: "JournalPrompt", predicate: predicate)
publicDatabase.perform(query, inZoneWith: nil) { [weak self] (records, error) in
if let error = error as? CKError {
if error.code == .unknownItem {
print("JournalPrompt record type does not exist or the specific record was not found in the public database.")
} else {
print("Error fetching record from iCloud public database: \(error)")
}
return
}
guard let record = records?.first else {
print("No record found with the specified ID in the public database.")
return
}
print("Found record in public database:")
print("Record ID: \(record.recordID.recordName)")
print("Text: \(record["text"] as? String ?? "No text")")
print("Creation Date: \(record.creationDate ?? Date())")
print("Used Count: \(record["usedCount"] as? Int ?? 0)")
print("Is Default: \(record["isDefault"] as? Bool ?? false)")
}
}
Error
When I run this code, I get the following error:
Error fetching record from iCloud public database: <CKError 0x600000c072a0: "Invalid Arguments" (12/1009); "Invalid predicate: recordKey (recordID.recordName) contains invalid characters">
I've also implemented a function to check the CloudKit schema:
func checkCloudKitSchema() {
checkDatabase(scope: .private)
checkDatabase(scope: .public)
}
private func checkDatabase(scope: CKDatabase.Scope) {
let container = CKContainer(identifier: "[CONTAINER_IDENTIFIER]")
let database = scope == .private ? container.privateCloudDatabase : container.publicCloudDatabase
print("Checking \(scope == .private ? "private" : "public") database")
database.fetchAllRecordZones { (zones, error) in
if let error = error {
print("Error fetching record zones: \(error)")
return
}
print("Available record zones in \(scope == .private ? "private" : "public") database:")
zones?.forEach { zone in
print("- \(zone.zoneID.zoneName)")
}
let query = CKQuery(recordType: "JournalPrompt", predicate: NSPredicate(value: true))
database.perform(query, inZoneWith: nil) { (records, error) in
if let error = error as? CKError, error.code == .unknownItem {
print("JournalPrompt record type does not exist in the \(scope == .private ? "private" : "public") database.")
} else if let error = error {
print("Error fetching records from \(scope == .private ? "private" : "public") database: \(error)")
} else if let records = records, !records.isEmpty {
print("JournalPrompt record type exists in the \(scope == .private ? "private" : "public") database.")
print("Fetched \(records.count) JournalPrompt records:")
for record in records {
print("Record ID: \(record.recordID.recordName)")
print("Fields:")
record.allKeys().forEach { key in
print(" - \(key): \(type(of: record[key]))")
}
print("---")
}
} else {
print("JournalPrompt record type exists in the \(scope == .private ? "private" : "public") database, but no records found.")
}
}
}
}
When I run this, I get:
Checking public database Available record zones in public database:
- _defaultZone JournalPrompt record type does not exist in the public database.
CloudKit Database Setup
I've set up my CloudKit Database as follows:
And my data model is as follows:
Despite this setup, my app can't seem to find or interact with the JournalPrompt record type. I've double-checked that my app's identifier matches the one in the CloudKit dashboard, and I've verified that the record type name is spelled correctly.
Questions:
- Why might my app be unable to find the JournalPrompt record type, even though it's defined in the CloudKit dashboard?
- Is there anything wrong with my query or error handling that could be causing this issue?
- Are there any common pitfalls or setup steps I might have missed when integrating CloudKit?
Any insights or suggestions would be greatly appreciated. I really appreciate any help you can provide.