About TableView

Hello
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?


Answered by OOPer in 633792022

The nextVC.title value in prepare function is set little bit late. 

Right. The methods tableView(_:cellForRowAt:) or prepare(for:sender:) are called already in the main thread.
(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.

I want to take it out from main queue. Can I do this?

Yes, you can. You have no need to or should not use Displatch.main.async {...} in methods which are called in the main thread.
(Unless you have special intention to utilize the queue's feature, like delaying execution.)
Accepted Answer

The nextVC.title value in prepare function is set little bit late. 

Right. The methods tableView(_:cellForRowAt:) or prepare(for:sender:) are called already in the main thread.
(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.

I want to take it out from main queue. Can I do this?

Yes, you can. You have no need to or should not use Displatch.main.async {...} in methods which are called in the main thread.
(Unless you have special intention to utilize the queue's feature, like delaying execution.)
No, you don't have to do it in cellForRowAt. Even if you set cell labels.

You don't need either in prepare.
BTW: how do you use VC Title in code ?
Thanks.
How can I check whether some function is called in main thread?
I just want to be clear when I should use main queue for UI stuff.
Thanks as well.
I just took VC from segue and used it.

I just want to be clear when I should use main queue for UI stuff. 

Generally, any methods called by iOS triggered by some user interaction are executed in the main thread.
This includes all the delegate methods of UIControls/UIViews or the lifecycle methods of the view controllers.

If any of such methods may be called within non-main thread, the doc for the method should contain some notes about it.
(But unfortunately, Apple's docs may not be reliable enough...)

An explicit and typical case you need DispatchQueue.main is for communication. Completion handlers passed to URLSession may be called in arbitrary threads. And you should use DispatchQueue.main when updating UI in such handlers.
About TableView
 
 
Q