altralaser
altralaser

Reputation: 2073

Table view does not update when calling from completion handler

I built a rename dialog to change selected list (table) entries. The rename dialog has a delagate in the main view. If the dialog is closed by clicking "save" then some database operations should be executed and the table must be refreshed:

func finishedShowing(_ vc : UIViewController, _ result : Bool) {
    if result {
        vc.dismiss(animated: true, completion: {
            if vc is RenameViewController {
                // do something in the database and update the table model
                self.refreshLibrary()
            }
        })
    }
}

private func refreshLibrary() {
    self.tableView.reloadData()
}

My problem is that the database is changed successfully so the completion handler is triggered correctly and also the table model (an array of strings in my case) is changed (I debugged it) but the table view does not update. It only shows the old version.

Upvotes: 0

Views: 209

Answers (1)

Kingalione
Kingalione

Reputation: 4265

Call the reload function inside the main thread:

private func refreshLibrary() {
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

Upvotes: 2

Related Questions