How to Utilize Fetched Record Attributes with CloudKit?

瘦欲@ 提交于 2020-01-17 03:06:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!