DispatchQueue:无法在非主线程上使用asCopy = NO调用

问题描述:

我在主线程上将UIAlertController表示为:

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)
        }
    }
}

我正在从任何UIViewController调用该方法:

And I am calling the method from any UIViewController as:

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

我正确地获得了输出.

但是在控制台中,我收到以下消息:

But in console I am getting below message:

[Assert]不能在非主线程上使用asCopy = NO调用.

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

修改

感谢@NicolasMiari:

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?

您应在主队列中调用showAlertMessage中的所有代码:

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)
    }
}