DispatchQueue:无法在非主线程上使用 asCopy = NO 调用
DispatchQueue : Cannot be called with asCopy = NO on non-main thread
我在主线程上将 UIAlertController
呈现为:
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
调用方法:
HelperMethodClass.showAlertMessage(message: "Any Message", viewController: self)
我正在正确获取输出。
但在控制台中我收到以下消息:
[Assert] Cannot be called with asCopy = NO on non-main thread.
我是不是哪里做错了,或者我可以忽略这条消息?
编辑
感谢@NicolasMiari:
添加以下代码未显示任何消息:
DispatchQueue.main.async {
HelperMethodClass.showAlertMessage(message: "Any Message", viewController: self)
}
之前它在控制台中显示消息的原因可能是什么?
您应该在主队列上调用 showAlertMessage
中的所有代码:
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)
}
}
我在主线程上将 UIAlertController
呈现为:
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
调用方法:
HelperMethodClass.showAlertMessage(message: "Any Message", viewController: self)
我正在正确获取输出。
但在控制台中我收到以下消息:
[Assert] Cannot be called with asCopy = NO on non-main thread.
我是不是哪里做错了,或者我可以忽略这条消息?
编辑
感谢@NicolasMiari:
添加以下代码未显示任何消息:
DispatchQueue.main.async {
HelperMethodClass.showAlertMessage(message: "Any Message", viewController: self)
}
之前它在控制台中显示消息的原因可能是什么?
您应该在主队列上调用 showAlertMessage
中的所有代码:
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)
}
}