0

Im trying get data from several dates, using the the same function. The function itself is working fine, what Im unable to do is get the order right. The arrays "restaurants", "yesterdayData", "lastWeekData" doesnt gets filled in on time and the tables come out empty. Mind you, I shortened some of the code but the I the order is the same and it I think you will get the gist of it.

        var general = [Results]()
        var restaurants = [Results]()
        var yesterdayData = [Results]()
        var lastWeekData = [Results]()
    
    
     func returnJson(goDate: String) -> Array<Results> {
                   
            Geting URl code... 
                
                let decoder = JSONDecoder()
                
                do {
                    let jsonPetitions = try decoder.decode(RootRequest.self, from: data)
                    self.general = jsonPetitions.results
           
                   DispatchQueue.main.async {
                      self.tableView.reloadData()
                       }
                } catch {
                    print(error)
                }
            }
         
            task.resume()
            return (general)
        }
        

override func viewDidLoad() {
        super.viewDidLoad()
      
  let dispatchGroup = DispatchGroup()

       dispatchGroup.enter()
       restaurants = returnJson(goDate: dateSelected)
       dispatchGroup.leave()

       dispatchGroup.enter()
       yesterdayData = returnJson(goDate: yesterdayDate)
       dispatchGroup.leave()

       dispatchGroup.enter()
       lastWeekData = returnJson(goDate: lastWeekDate)
       dispatchGroup.leave()
 
}

What Im doing wrong? Thank you

0 Answers0