如何利用CloudKit利用提取的记录属性?

时间:2016-05-29 04:59:58

标签: ios swift cocoa cloudkit

所以我尝试使用CloudKit查询我的公共数据库中的单个记录,并将记录中的属性值保存到外部数组(foodArrayLunch)。我在闭包中使用属性RecordFetchedBlock来处理我的记录。但是,当闭包执行成功时,我发现我的数组无法保存从数据库中查询的属性值,并且只能从闭包内部访问和管理它。所以我的问题是:如何从RecordFetchedBlock外部保存此属性值?任何帮助将不胜感激,谢谢!

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)

0 个答案:

没有答案