DispatchQueue : Cannot be called with asCopy = NO on non-main thread

不问归期 提交于 2020-06-24 10:58:28

问题


I am presenting the UIAlertController on the main thread as :

class HelperMethodClass: NSObject {

    class func showAlertMessage(message:String, viewController: UIViewController) {
        let alertMessage = UIAlertController(title: "", message: message, preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: "Ok", style: .cancel)

        alertMessage.addAction(cancelAction)

        DispatchQueue.main.async {
            viewController.present(alertMessage, animated: true, completion: nil)
        }
    }
}

And I am calling the method from any UIViewController as:

HelperMethodClass.showAlertMessage(message: "Any Message", viewController: self)

I am getting the output properly.

But in console I am getting below message:

[Assert] Cannot be called with asCopy = NO on non-main thread.

Is there something I have done wrong here or I can ignore this message ?

Edit

Thanks to @NicolasMiari :

Adding below code is not showing any message:

DispatchQueue.main.async {
    HelperMethodClass.showAlertMessage(message: "Any Message", viewController: self)
}

What can be the reason that previously it was showing the message in console?


回答1:


You should call all code from showAlertMessage on main queue:

class func showAlertMessage(message:String, viewController: UIViewController) {
    DispatchQueue.main.async {
        let alertMessage = UIAlertController(title: "", message: message, preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: "Ok", style: .cancel)

        alertMessage.addAction(cancelAction)

        viewController.present(alertMessage, animated: true, completion: nil)
    }
}


来源:https://stackoverflow.com/questions/52808945/dispatchqueue-cannot-be-called-with-ascopy-no-on-non-main-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!