部署更改为生产时,如何在CKRecord中为字段添加默认值?

时间:2017-01-11 19:22:42

标签: ios swift cloudkit

问题在于:

目前,我在开发模式的云仪表板中有Service记录类型:

enter image description here

但第一个版本是没有 createdAt字段。

我确实将第一个版本部署到生产模式,这很好。然后我通过添加Service字段更改了createdAt。我确实将它部署到生产中。所以在制作中我有这样的字段:

enter image description here

没有createdAt日期。

当我开发应用程序并尝试获取所有Service条记录时......一切都很好。它们被取出并在应用程序中工作。因此,我将更改部署到生产模式,将应用程序提交到应用商店。 Apple确实对它进行了审核......并且......它无法正常工作。的 WHY吗

他们没有默认的createdAt值...当我获取所有这些值时......没有提取任何内容(因为应用中没有任何内容)。

但是...

当我在 PRODUCTION MODE 中手动更新createdAt时,您可以看到:

enter image description here

然后AppStore中的应用程序运行正常,这些记录被提取并显示在应用程序中。

可能是因为它们没有出现在应用程序中? 我可以以某种方式为那些目前在云中的人设置默认值吗?

我要更新638条记录:(

enter image description here

1 个答案:

答案 0 :(得分:1)

由于您告诉我必须使用自定义createdAt日期,而不是CKRecord的自然creationDate属性,您应该可以执行以下操作:

func getServiceRecords() {
    let predicate:NSPredicate = NSPredicate(value: true)
    let query:CKQuery = CKQuery(recordType: "Service", predicate: predicate)

    // Create an empty array of CKRecords to append ones without createdAt value
    var empty:[CKRecord] = []

    // Perform the query
    if let database = self.publicDatabase {

        database.perform(query, inZoneWith: nil, completionHandler: { (records:[CKRecord]?, error:Error?) -> Void in

            // Check if there is an error
            if error != nil {

            }
            else if let records = records {

                for record in records {
                    if let _ = record.object(forKey: "createdAt") as! Date? {
                        // This record already has assigned creationDate and shouldnt need changing
                    } else {
                        // This record doesn't have a value create generic one and append it to empty array
                        record.setObject(Date() as CKRecordValue?, forKey: "createdAt")
                        empty.append(record)
                    }
                }

                self.saveCustomCreationDates(records: empty)
            }

        })

    }
}

func saveCustomCreationDates(records: [CKRecord]) {
    if let database = self.publicDatabase {

        // Create a CKModifyRecordsOperation
        let operation = CKModifyRecordsOperation(recordsToSave: records, recordIDsToDelete: nil)
        operation.savePolicy = .allKeys
        operation.qualityOfService = .userInteractive
        operation.allowsCellularAccess = true
        operation.modifyRecordsCompletionBlock = { (records:[CKRecord]?, deleted:[CKRecordID]?, error:Error?) in
            if error != nil {
               // Handle error
            }
            else if let records = records {

                for record in records {

                    if let creationDate = record.object(forKey: "createdAt") as! Date? {
                        // You can verify it saved if you want
                        print(creationDate)
                    }
                }

            }

        }
        // Add the operation
        database.add(operation)
    }

}