如何在加载TableData之前从CloudKit获取数据

时间:2015-08-27 19:15:46

标签: ios iphone swift tableview

我正在Swift中编写一个应用程序,其中第一个场景有一个TableView,我设置它来显示标题并且它工作正常,我还设置了计算CloudKit数据库中的出现次数(或任何其调用的)但它在异步中执行计数,因此表默认在详细信息窗格中显示0。

我需要知道如何让应用在设置正确细节的值之前等待,直到完成计数或之后如何更改它们。

我附上了我用于执行计数等的代码,如果我这样做错误或效率低下请告诉我

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true;

    self.textArray.addObject("Link 300")
    self.textArray.addObject("Link 410")
    self.textArray.addObject("Link 510")

    let container = CKContainer.defaultContainer()
    let publicData = container.publicCloudDatabase

    let query = CKQuery(recordType: "Inventory", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))

    publicData.performQuery(query, inZoneWithID: nil){results, error in
        if error == nil {
            for res in results {
                let record: CKRecord = res as! CKRecord

                if(record.objectForKey(("TrackerModel")) as! String == "Link 300"){
                    self.count300 = self.count300++
                }else if(record.objectForKey(("TrackerModel")) as! String == "Link 410"){
                    self.count410 = self.count410++
                }else if(record.objectForKey(("TrackerModel")) as! String == "Link 510"){
                    self.count510 = self.count510++
                }
            }
        }else{
            println(error)
        }
    }

    self.detailArray.addObject(self.count300.description)
    self.detailArray.addObject(self.count410.description)
    self.detailArray.addObject(self.count510.description)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.textArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
    var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    cell.textLabel?.text = self.textArray.objectAtIndex(indexPath.row) as? String
    cell.detailTextLabel?.text = self.detailArray.objectAtIndex(indexPath.row) as? String
    return cell
}

非常感谢 - 罗比

1 个答案:

答案 0 :(得分:1)

performQuery关联的闭包将异步完成 - 即viewDidLoad完成后。您需要确保在查询完成后重新加载表视图并获得数据。您也遇到问题,因为您正在更新闭包之外的总计 - 此代码也将在数据加载之前执行。

最后,确保在主队列上调度对UI的任何更新(例如重新加载表视图)

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true;

    self.textArray.addObject("Link 300")
    self.textArray.addObject("Link 410")
    self.textArray.addObject("Link 510")

    let container = CKContainer.defaultContainer()
    let publicData = container.publicCloudDatabase

    let query = CKQuery(recordType: "Inventory", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))

    publicData.performQuery(query, inZoneWithID: nil){results, error in
        if error == nil {
            for res in results {
                let record: CKRecord = res as! CKRecord

                if(record.objectForKey(("TrackerModel")) as! String == "Link 300"){
                    self.count300++
                }else if(record.objectForKey(("TrackerModel")) as! String == "Link 410"){
                    self.count410++ 
                }else if(record.objectForKey(("TrackerModel")) as! String == "Link 510"){
                    self.count510++
                }
            }
            self.detailArray.addObject(self.count300.description)
            self.detailArray.addObject(self.count410.description)
            self.detailArray.addObject(self.count510.description)
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
            })
        }else{
            println(error)
        }
    }
}