问题
so I'm trying to query my public database for a single record using CloudKit, and save an attribute value from the record to an external array(foodArrayLunch). I'm using the property RecordFetchedBlock in a closure to process my record. However, when the closure executes successfully, I find that my array failed to save the attribute value that was queried from the database, and it can be accessed and managed only from inside the closure. So my question is: How can I save this attribute value from outside of the RecordFetchedBlock? Any help would be greatly appreciated, Thanks!
class ViewControllerNewcomb: UIViewController, UITableViewDelegate, UITableViewDataSource {
let database = CKContainer.defaultContainer().publicCloudDatabase
@IBOutlet weak var LunchView: UITableView!
var foodArrayLunch: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)
let query = CKQuery(recordType: "FoodData", predicate: predicate)
// get just one value only
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["FoodList"]
// get query
operation.recordFetchedBlock = { (record : CKRecord) in
let menu = record.objectForKey("FoodList") as! [String]
for item in menu {
self.foodArrayLunch.append(item)
}
}
// operation completed
operation.queryCompletionBlock = {(cursor, error) in
dispatch_async(dispatch_get_main_queue()) {
if error == nil {
print("no errors")
} else {
print("error description = \(error?.description)")
}
}
}
database.addOperation(operation)
来源:https://stackoverflow.com/questions/37506360/how-to-utilize-fetched-record-attributes-with-cloudkit