title: Swift iOS update Chart with data from Database
body: <p>I am currently learning how to connect to a SQL DataBase and displaying the information. I also am trying to grasp closures at the same time as well as building charts. So far I can successfully pull the data from the database then display the information in a <code>UITableViewController</code>. </p>
  
  <p>I have a simple <code>UIViewController</code> with a <code>UIView</code> to display the data. So far my issue is all the data is pulled inside of the closure, but then when I do anything outside of the closure there is no data. </p>
  
  <pre><code> @IBOutlet weak var lineChartView: LineChartView!
  
      var sensorReadings: [SensorReadings] = []
  
      var lineChartEntry = [ChartDataEntry]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let url = URL(string: "replaced with actual URL ")
  
          URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
              guard let data = data, error == nil else { print(error!); return }
  
              if error != nil {
                  print("Datatask Error: ", error!)
              } else {
                  do {
                      if let sensorData = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:String]] {
                          for sData in sensorData {
                              let dataId = sData["dataid"] ?? ""
                              let readingTime = sData["readingtime"] ?? ""
                              let sensorId = sData["sensorid"] ?? ""
                              let reading = sData["reading"] ?? ""
  
                              self.sensorReadings.append(SensorReadings(dataId: dataId, readingTime: readingTime, sensorId: sensorId, reading: reading))
                          }
                      }                    
                  } catch {
                      print("Serialization Error ", error)
                  }
              }
              }).resume()
  
          // Creates an array of strings to hold reading values
          var tempArray: [String] = []
  
          // Loops through the downloadaed data
          // Adds each of the readings to tempArray
          for reading in self.sensorReadings {
              tempArray.append(reading.reading)
          }
  
          // Convert String to Double
          let doublesArray = tempArray.compactMap(Double.init)
  
          // Chart Information
          for i in 0..&lt;doublesArray.count {
              let value = ChartDataEntry(x: Double(i), y: doublesArray[i])
  
              self.lineChartEntry.append(value)
          }
  
          let line = LineChartDataSet(entries: self.lineChartEntry, label: "Number")
          line.colors = [NSUIColor.blue]
  
          let chartData = LineChartData()
          chartData.addDataSet(line)
  
          self.lineChartView.data = chartData
  
          self.lineChartView.chartDescription?.text = "Data"
  
      }
  
  </code></pre>
  
  <p>First am I on the right track with getting the data from the database? Then how is it that I can update the Chart with the information? How to I return the information from the URLSession? Or is there a way I can update the chart like I do in a `UITableView1: </p>
  
  <pre><code>    DispatchQueue.main.async {
          self.tableView.reloadData()
      }
  </code></pre>