I have a problem with fetching a record from iCloud. I want to access a fetched record and assign one value from the key-value pair.
I already tried:
currentRecord.object(forKey:)
currentRecord.value(forKey:)
currentRecord.value(forKeyPath:
Here is my fetchRecord method:
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
}
}
}
Result of:
print("\(self.currentRecord)")
is:
{
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 -> "[email protected]"
companyName -> "PKP"
companyZipCode -> "12356"
})
Everything seems fine with the record
print("ALL KEYS: \(self.currentRecord.allKeys())")
Result:
ALL KEYS: ["companyStreet", "companyCity", "companyPhone", "companyEmail", "companyName", "companyZipCode"]
If I want to assign the value of the key "companyStreet" to my label, it returns 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
EDIT -> SOLVED I finally solved my problem. The problem was my appDelegate, trying to handle my CKNotification. Instead of referencing to my existing TableViewController (embedded in a Navigation Controller - TabBarController) I created a new instance of my TableViewController.