用健康数据填充UITableView?

时间:2015-10-21 10:06:46

标签: ios swift uitableview asynchronous health-kit

我正在尝试使用Health应用程序中的数据填充我的tableview。问题是每次加载屏幕时这些数据的顺序都不同。

我有从Health应用程序获取数据的功能:

func readWalkrunDistance(startDate: NSDate) {
    healthManager.readRunningAndWalkingDistance(startDate, endDate: selectedDate!) { (results, error) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            var dict: [String: Double] = [String: Double]()
            dict["Walkrun Distance"] = results
            dict["number"] = 13
            if let result = results {
                self.walkrunDistance = result
            }
            self.healthData.append(dict)
            self.updateData()
        })
    }
}

func readBodyMass(startDate: NSDate) {
    healthManager.readMedianBodyMass(startDate, endDate: selectedDate!) { (results, error) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            var dict: [String: Double] = [String: Double]()
            dict["Body Mass"] = results
            dict["number"] = 7
            if let result = results {
                self.bodyMass = result
            }
            self.healthData.append(dict)
            self.updateData()
        })
    }
}

另外10个功能就像其他健康数据一样。问题是数据每次都以不同的顺序出现,有时walkrunDistance会显示两次等等。我认为这是因为异步调用,但我不知道如何等待这些函数获取数据,然后去to cellForRowAtIndexPath ...

这是我调用所有Health函数的函数:

func readAllData() {
    selectedDate = ProjectManager.sharedInstance.chosenDate
    selectedDate = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: 1, toDate: selectedDate, options: [])
    let startDate = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -1, toDate: selectedDate, options: [])
    self.readActiveEnergyBurned(startDate!)
    self.readBasalEnergyBurned(startDate!)
    self.readBloodAlcoholContent(startDate!)
    self.readBloodPressureDiastolic(startDate!)
    self.readBloodPressureSystolic(startDate!)
    self.readBodyFatPercentage(startDate!)
    self.readBodyMass(startDate!)
    self.readDistanceCycling(startDate!)
    self.readFlightsClimbed(startDate!)
    self.readAverageHeartRate(startDate!)
    self.readMinimumHeartRate(startDate!)
    self.readMaximumHeartRate(startDate!)
    self.readLeanBodyMass(startDate!)
    self.readStepCount(startDate!)
    self.readWorkouts(startDate!)
    self.tableView.reloadData()
}

我想首先获取数据,然后以正确的方式将其加载到tableview中。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用dispatch_group。

这是指向教程的链接:

http://www.raywenderlich.com/79150/grand-central-dispatch-tutorial-swift-part-2

  • 只需将var group = dispatch_group_create()变量添加到您的课程中即可。
  • dispatch_group_enter(group)添加到您的所有功能中,并dispatch_group_leave(group)添加到异步调用的末尾。

    func readWalkrunDistance(startDate: NSDate) { healthManager.readRunningAndWalkingDistance(startDate, endDate: selectedDate!) { (results, error) -> Void in dispatch_group_enter(group) dispatch_async(dispatch_get_main_queue(), { () -> Void in var dict: [String: Double] = [String: Double]() dict["Walkrun Distance"] = results dict["number"] = 13 if let result = results { self.walkrunDistance = result } self.healthData.append(dict) self.updateData() dispatch_group_enter(group) }) } }

  • self.tableView.reloadData() - dispatch_group_wait(group, DISPATCH_TIME_FOREVER)

  • 之前添加

这将解决您的“异步”问题。所有功能都将调用-reloadData