为啥我无法访问 CloudKit 中 CKRecord 的 createdAt 字段?
Posted
技术标签:
【中文标题】为啥我无法访问 CloudKit 中 CKRecord 的 createdAt 字段?【英文标题】:Why can I not access the createdAt field of my CKRecord in CloudKit?为什么我无法访问 CloudKit 中 CKRecord 的 createdAt 字段? 【发布时间】:2018-12-07 14:19:16 【问题描述】:我无法在 CloudKit 中访问我的记录类型的 createdAt 系统字段。我已经尝试将 'createdAt' 和 'creationDate' 作为 CKQueryOperation.desiredKeys 参数的参数。无论哪种方式,当我使用 CKRecord.object(forKey:) 访问 createdAt 字段时,我都会得到 nil。
这是我检索记录的代码:
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)
这是保存键的字符串变量的代码:
enum DatabaseNameStrings
static let recordTypeEntry = "Entry"
static let fieldNameCreatedAt = "createdAt"
static let fieldNameText = "text"
static let recordFieldKeyCreationDate = "creationDate"
这是我访问 createdAt 字段的代码:
print(ckRecord.object(forKey: DatabaseNameStrings.recordFieldKeyCreationDate) as Any)
print(ckRecord.object(forKey: DatabaseNameStrings.fieldNameCreatedAt) as Any)
【问题讨论】:
【参考方案1】:我使用 CKRecord.creationDate 而不是 CKRecord.object(forKey:)。
【讨论】:
以上是关于为啥我无法访问 CloudKit 中 CKRecord 的 createdAt 字段?的主要内容,如果未能解决你的问题,请参考以下文章
无法从应用程序扩展访问我的“CKContainer”中的任何数据 - CloudKit,Swift
无法访问 CloudKit 出现奇怪错误:内部错误 (1/2005)