ios - 无法加载图表数据

标签 ios swift firebase charts ios-charts

我正在为我的应用程序使用图表框架。如果我给它一些静态数据,我可以很容易地将值放入图表中。但是当我尝试从 Firebase 获取数据时,似乎在数据获取完成之前调用了 setChart 函数。

有什么建议吗?正在从 ViewDidLoad() 调用我的 GetLogs() 函数

var dates = [String]()
var values = [Double]()

func getLogs() {
    DataService.ds.REF_PERIODS.child(period.periodId).child("logs").observeSingleEvent(of: .value, with: {(userSnap) in

        if let SnapDict = userSnap.value as? [String:AnyObject]{
            //Run through all the logs
            for each in SnapDict {
                DataService.ds.REF_LOGS.child(each.key).observeSingleEvent(of: .value , with : {(Snap) in

                    let postData = Snap.value as! Dictionary<String, AnyObject>
                    let log = Log(logId: each.key, postData: postData)

                    self.logs.append(log)

                    //Add the date to the the dates and values
                    self.dates.append(log.date)
                    self.values.append(Double(log.measurement)!)

                    //Sorts the logs by date
                    self.logs.sort(by: {$0.date > $1.date})
                    self.tableView.reloadData()

                })
            }
        }
        //Set the charts with the given information
        self.setChart(dataPoints: self.dates, values: self.values)
    })
}

但它目前无法显示来自 Firebase 的任何内容。

最佳答案

您的代码结构不正确。您有一个对 FireBase 的外部异步调用。在该异步调用中,您有一个 for 循环,它对响应字典中的每个项目执行另一个异步调用。但是,您尝试在内部异步调用完成之前设置您的图表,这是行不通的。

编辑:

由于您在 for 循环中产生了一大堆异步调用,因此您需要逻辑来跟踪有多少内部调用已完成,并且仅在最后一个调用完成加载后才更新您的图表。像这样:

var dates = [String]()
var values = [Double]()

func getLogs() {
  DataService.ds.REF_PERIODS.child(period.periodId).child("logs").observeSingleEvent(of: .value, with: {(userSnap) in
    
    if let SnapDict = userSnap.value as? [String:AnyObject]{
      var completedCount = 0 //Keep track of how many items has been completed.
      //Run through all the logs
      for each in SnapDict {
        DataService.ds.REF_LOGS.child(each.key).observeSingleEvent(of: .value , with : {(Snap) in
          
          let postData = Snap.value as! Dictionary<String, AnyObject>
          let log = Log(logId: each.key, postData: postData)
          
          self.logs.append(log)
          
          //Add the date to the the dates and values
          self.dates.append(log.date)
          self.values.append(Double(log.measurement)!)
          
          //Sorts the logs by date
          self.logs.sort(by: {$0.date > $1.date})
          completedCount += 1
          if completedCount == snapDict.count {
            self.tableView.reloadData()
            //Set the charts with the given information
            self.setChart(dataPoints: self.dates, values: self.values)
          }
          
        })
      }
    }
  })
}

关于ios - 无法加载图表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41382920/

相关文章:

ios - tableView.reloadData() 只调用一次

javascript - $createUser(credentials) - Firebase 创建 Slack 的克隆

ios - CocoaPods 的 Pods.xcconfig 与现有的冲突

ios - authorizationStatus 返回一个 bool 值

ios - 在 iOS 中将 CFBundleTypeIconFiles 的图像文件放在哪里?

firebase - 如何查看收藏夹中的内容?

android - 从 firebase 获取所有 child (用户)但向一个 child (用户)显示按钮?

ios - 在运行时动态创建核心数据实体

swift - 是否可以在 Swift 中以相同的名称存储一个字符串和一个数组?

xcode - .xib 中用于自定义类的垂直 UISlider?