Hello
I heard that I have to use DispatchQueue.main queue for setting UI stuff. So I used it like this.
Is this right code?
The nextVC.title value in prepare function is set little bit late. So I want to take it out from main queue. Can I do this? or Is there any better solution?
I heard that I have to use DispatchQueue.main queue for setting UI stuff. So I used it like this.
Code Block swift func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell", for: indexPath) as! CountryListTableViewCell DispatchQueue.main.async { cell.countryNameLabel.text = self.countries[indexPath.row].name cell.countryImageView.image = UIImage(named: "flag_\(self.countries[indexPath.row].asset)") } cell.nameOfDataAsset = countries[indexPath.row].asset return cell } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard let nextVC = segue.destination as? WeatherListViewController else { return } guard let cell = sender as? CountryListTableViewCell else { return } DispatchQueue.main.async { nextVC.title = cell.countryNameLabel.text } nextVC.nameOfDataAsset = cell.nameOfDataAsset }
Is this right code?
The nextVC.title value in prepare function is set little bit late. So I want to take it out from main queue. Can I do this? or Is there any better solution?
Right. The methods tableView(_:cellForRowAt:) or prepare(for:sender:) are called already in the main thread.The nextVC.title value in prepare function is set little bit late.
(I assume you have no code to call such methods explicitly from non-main thread.)
In that context, codes inside Displatch.main.async {...} are delayed.
Yes, you can. You have no need to or should not use Displatch.main.async {...} in methods which are called in the main thread.I want to take it out from main queue. Can I do this?
(Unless you have special intention to utilize the queue's feature, like delaying execution.)