Reputation: 966
I am not able to access the createdAt system field of my record type in CloudKit. I have tried both 'createdAt' and 'creationDate' as the parameter of the CKQueryOperation.desiredKeys argument. Either way, when I access the createdAt field using CKRecord.object(forKey:), I get nil.
Here is my code to retrieve the records:
entries = [Entry]()
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeEntry, predicate: predicate)
let sortDescriptor = NSSortDescriptor(key: DatabaseNameStrings.recordFieldKeyCreationDate, ascending: true)
query.sortDescriptors = [sortDescriptor]
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = [DatabaseNameStrings.recordFieldKeyCreationDate, DatabaseNameStrings.fieldNameText]
queryOperation.recordFetchedBlock = {
(record: CKRecord) in
let entry = Entry(ckRecord: record)
self.entries.append(entry)
}
queryOperation.queryCompletionBlock = {
(cursor: CKQueryOperation.Cursor?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else {
DispatchQueue.main.async {
self.tableView.reloadData()
}
if let cursor = cursor {
let innerQueryOperation = CKQueryOperation(cursor: cursor)
innerQueryOperation.desiredKeys = queryOperation.desiredKeys
innerQueryOperation.recordFetchedBlock = queryOperation.recordFetchedBlock
innerQueryOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
queryOperationQueue.addOperation(queryOperation)
}
}
}
queryOperationQueue.addOperation(queryOperation)
Here is the code to the string variables that hold the keys:
enum DatabaseNameStrings {
static let recordTypeEntry = "Entry"
static let fieldNameCreatedAt = "createdAt"
static let fieldNameText = "text"
static let recordFieldKeyCreationDate = "creationDate"
}
Here is the code where I access the createdAt field:
print(ckRecord.object(forKey: DatabaseNameStrings.recordFieldKeyCreationDate) as Any)
print(ckRecord.object(forKey: DatabaseNameStrings.fieldNameCreatedAt) as Any)
Upvotes: 2
Views: 337