我想從queryCompletionBlock
中檢索一個值並將其發送給另一個viewController
。如何從完成塊返回值以在其他viewController中使用它?
這是我的代碼:
func KcalCloudKitData() {
publicDatabase = container.publicCloudDatabase
allRecords = []
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Kcal", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "KcalCoef", ascending: true)]
let queryOperation = CKQueryOperation(query: query)
queryOperation.recordFetchedBlock = {(record: CKRecord) in
self.allRecords.append(record)
}
queryOperation.queryCompletionBlock = {cursor, error in
if error != nil {
print("Failed loading data from iCloud")
//print(error?.localizedFailureReason)
} else {
for value in self.allRecords {
self.kcalUnitValid.append(value.object(forKey: "KcalValidUnit") as! [String])
}
//self.performSegue(withIdentifier: "AppLoaded", sender: nil)
}
}
print(self.kcalUnitValid)
publicDatabase?.add(queryOperation)
}
當我打印使用完成塊外部碼print(self.kcalUnitValid)
此獲得一個空表。任何解決方案
在此先感謝。
感謝威爾遜的幫助。有效。這是最好的解決方案。 – Patrick
很高興幫助你! – Wilson