Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 377
Amit Avatar asked Oct 15 '18 02:10

Amit


1 Answers

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)     } } 
like image 144
Ilya Kharabet Avatar answered Sep 19 '22 21:09

Ilya Kharabet