Error updating TableViewCell with a function that gets data from URLSession

Viewed 29

I have a table view cell which should get JSON data from an API and display it in the cell but it's empty. If I declare the same data locally and use it, it displays correctly.

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = .clear

    //createArray()

    getBinData(userCompletionHandler: { data in
        if let data = data {
            self.createArray()
        }
    })

    tableView.delegate = self
    tableView.dataSource = self
    self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none

    // Do any additional setup after loading the view.
}

Here is getBinData that basically gets the data from some API:

func getBinData(userCompletionHandler: @escaping (JsonObjectiveListClass?) -> Void) {

    let session = URLSession.shared
    let url = URL(string: "https://api.jsonbin.io/b/randombin/1")
    let task = session.dataTask(with: url!, completionHandler: { (data, response, error) in

        guard let data = data else {return}
        do {
            let welcome = try? JSONDecoder().decode(JsonObjectiveListClass.self, from: data)
            userCompletionHandler(welcome)
        }

    })
    task.resume()    
}

func createArray() {
    var tempObjectives: [Objective] = []
    let image = getImageFromUrl(imageurl:  "https://www.image.com")
    let objective1 = Objective(image: image, title: "falcao TOTSSF", objectives: ["hello", "hello2"])
    tempObjectives.append(objective1)
    let objective2 = Objective(image:  #imageLiteral(resourceName: "sabitzer"), title: "Sabitzer TOTSSF",objectives: ["hello", "hello2"])
    tempObjectives.append(objective2)

    objectives = tempObjectives
}
2 Answers

You need to reload your tableView on main thread once you get data from server ... so update you code getBinData section and hopefully it will work

getBinData(userCompletionHandler: {[weak self] data in
        if let data = data {
            self?.createArray()
           DispatchQueue.main.async { 
            self?.tableView.reloadData()
           }
        }
    })

Try This

func createArray() {
    let dGroup = DispatchGroup()

    dGroup.enter()
    var tempObjectives: [Objective] = []
    let image = getImageFromUrl(imageurl:  "https://www.image.com")
    let objective1 = Objective(image: image, title: "falcao TOTSSF", objectives: ["hello", "hello2"])
    tempObjectives.append(objective1)
    let objective2 = Objective(image:  #imageLiteral(resourceName: "sabitzer"), title: "Sabitzer TOTSSF",objectives: ["hello", "hello2"])
    tempObjectives.append(objective2)
    objectives = tempObjectives
    dGroup.leave()

    dGroup.notify(queue: .main) {
        self.tableView.reloadData()
    }
}
Related