【问题标题】:CKError when querying system field: rejected "Unknown Field 'createdAt'"查询系统字段时出现CKError:拒绝“未知字段'createdAt'”
【发布时间】:2026-01-17 12:05:01
【问题描述】:

我正在像这样对createdAt 系统字段进行排序:

query.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]

并得到以下错误:

CKError 0x1c4447fb0:“无效参数”(12/2018);服务器消息= “未知字段‘createdAt’”; uuid = 9C450848-2449-4892-93BC-C46363203042;容器 ID = "...

我能够查询和排序我以这种方式创建的字段。系统领域有什么不同吗? 该字段的索引是可排序和可查询的。

【问题讨论】:

    标签: cloudkit ckquery


    【解决方案1】:

    改为使用creationDate 作为密钥。 CK 仪表板中的元键与用于查询的元键略有不同。以下是元键列表:

    recordID: CKRecordID
    The unique ID of the record.
    
    recordType: String
    The app-defined string that identifies the type of the record.
    
    creationDate: Date?
    The time when the record was first saved to the server.
    
    creatorUserRecordID: CKRecordID?
    The ID of the user who created the record.
    
    modificationDate: Date?
    The time when the record was last saved to the server.
    
    lastModifiedUserRecordID: CKRecordID?
    The ID of the user who last modified the record.
    
    recordChangeTag: String?
    A string containing the server change token for the record.
    

    【讨论】:

    • 成功了。谢谢!知道为什么元键与 CK 仪表板上的不同吗?
    • 很高兴为您提供帮助。不知道为什么键本身不同。但我记得在 CloudKit 谓词和排序描述符上苦苦挣扎,以至于在某个时候创建​​了一个实际键的列表,并将它们保存到我的 Notes 应用程序中。 :) /original/ CloudKit 仪表板中的元字段标签可能与键匹配,但我记起来太长了。
    • @BrianHamm 为什么当我尝试使用“createdAt”或“creationDate”访问该文件时,我无法使用 CKRecord.object(forKey:) 访问该文件?如何访问 creationAt 字段的值?
    • @DanielBrower 元字段值应作为属性访问。试试myRecord.creationDate
    • 哇,这太荒谬了..为什么在仪表板中我看到“modifiedAt”并将其设置为“可排序”,然后我必须使用“modificationDate”?!顺便说一句,谢谢你解决了这个问题。