UITableView 中带有警报的奇怪延迟

Strange delay with alert in UITableView

我有一些 UITableView 来自内部 array 的数据。我想在点击时显示 UIAlertController,但我遇到了非常奇怪的延迟。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    print("tapped \(dispatcher.conversations[indexPath.row].name)") //this one works fine, no problems here

    let message = dispatcher.conversations[indexPath.row].desc + "\n\nDo you wanna play this?"
    let alertname = dispatcher.conversations[indexPath.row].name

    let alert = UIAlertController(title: alertname, message: message, preferredStyle: .alert)

    let actionOK = UIAlertAction(title: "Play", style: UIAlertActionStyle.default, handler: { (action) in
        //play the file
    })

    let actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) in
        //cancel the file
    })

    alert.addAction(actionOK)
    alert.addAction(actionCancel)

    self.present(alert, animated: true, completion: {
        //some code here
    })

我的第一个警报有一些延迟,但基本上没问题。但是如果我试图点击下一个单元格,我必须等待几秒钟才能显示我的警报。

所以,似乎我在访问我的数据方面没有任何问题(打印工作正常),但不知何故之后需要几秒钟才能显示 UIAlertController。

我做错了什么?

改为将其显示在您的主队列中:

DispatchQueue.main.async(execute: {
    self.present(alert, animated: true, completion: {
    //some code here
    })
})