CloudKit使用CKRecordID获取记录

时间:2017-02-24 17:31:30

标签: cloudkit ckrecord ckfetchrecordchangesopera

从iCloud获取记录时遇到问题。我想访问一个获取的记录并从键值对中分配一个值。

我已经尝试过:

currentRecord.object(forKey:)
currentRecord.value(forKey:)
currentRecord.value(forKeyPath:

这是我的fetchRecord方法:

public func fetchRecord(_ recordID: CKRecordID) -> Void {
        let privateDatabase = CKContainer.default().privateCloudDatabase
        var recordIDArray: [CKRecordID] = []
        recordIDArray.append(recordID)
        let fetchRecordsWithID = CKFetchRecordsOperation(recordIDs: recordIDArray)
        fetchRecordsWithID.fetchRecordsCompletionBlock = { (records, error) in
            if error != nil {
                print("Error")
            } else {
                DispatchQueue.main.async() {
                    self.currentRecord = records?[recordID]
                    print("\(self.currentRecord)")
                    print("ALL KEYS: \(self.currentRecord.allKeys())")

                    self.companyNameLabel.text = self.currentRecord.value(forKeyPath: "companyName") as? String// as? String //object(forKey: "companyName") as? String
//                    self.companyStreetLabel.text = record!.object(forKey: "companyStreet") as? String
//                    self.companyZipCodeLabel.text = record!.object(forKey: "companyZipCode") as? String
//                    self.companyCityLabel.text = record!.object(forKey: "companyCity") as? String
//                    self.companyPhoneLabel.text = record!.object(forKey: "companyPhone") as? String
//                    self.companyEmailLabel.text = record!.object(forKey: "companyEmail") as? String

                }

            }
        }

结果:

print("\(self.currentRecord)")

是:

{
    creatorUserRecordID -> <CKRecordID: 0x170021800; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>
    lastModifiedUserRecordID -> <CKRecordID: 0x17002d420; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>
    creationDate -> 2017-02-24 16:39:21 +0000
    modificationDate -> 2017-02-24 17:18:54 +0000
    modifiedByDevice -> iCloud
    companyStreet -> "Street"
    companyCity -> "City"
    companyPhone -> "+49 123456136"
    companyEmail -> "info@abc.de"
    companyName -> "PKP"
    companyZipCode -> "12356"
})

记录中的一切似乎都很好

print("ALL KEYS: \(self.currentRecord.allKeys())")

结果:

ALL KEYS: ["companyStreet", "companyCity", "companyPhone", "companyEmail", "companyName", "companyZipCode"]

如果我想分配密钥的价值&#34; companyStreet&#34;在我的标签上,它返回nil:

fatal error: unexpectedly found nil while unwrapping an Optional value
2017-02-24 18:19:55.239907 XYZ[4868:976786] fatal error: unexpectedly found nil while unwrapping an Optional value

编辑 - &gt;解决 我终于解决了我的问题。问题是我的appDelegate,试图处理我的CKNotification。我没有引用现有的TableViewController(嵌入在导航控制器 - TabBarController中),而是创建了一个新的TableViewController实例。

0 个答案:

没有答案