Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss modal view controller freezes the app once in a while, swift 3

In my app I have a view controller, that I present modally. In this view controller I have a table view. Whenever user makes a selection in a table view, I dismiss the view controller.

The problem is that sometimes the view controller is not getting dismissed or getting dismissed after a long delay (5-7 seconds) even though the dismiss function is called.

Here is my code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if tableView == self.quarterTableView
    {
        self.delegate?.modalViewController(modalVC: self, dismissedWithValue:self.quarterPeriods[indexPath.row])
    }
    else if tableView == self.monthTableView
    {
        self.delegate?.modalViewController(modalVC: self, dismissedWithValue: self.monthPeriods[indexPath.row])
    }

    Print("didSelectRowAt dismiss")

    self.dismiss(animated: true) { 
        Print("finished")
    }
}

Any help is highly appreciated.

EDIT:

I resolved the issue by using:

DispatchQueue.main.async
{
    self.dismiss(animated: true) {
       DDLogDebug("finished")
    }
}

Is there any harm done by doing so?

like image 850
Eugene Gordin Avatar asked Dec 06 '22 15:12

Eugene Gordin


2 Answers

if you want something on the UI to happen right away, execute it on the main queue

DispatchQueue.main.async(execute: {
    self.dismiss(animated: true) { 
    Print("finished")
})
like image 58
Russell Avatar answered Apr 29 '23 01:04

Russell


try to use a dispatch DispatchQueue

DispatchQueue.main.async(execute: {

})
like image 44
sbsr Avatar answered Apr 29 '23 01:04

sbsr