Dispatchqueue:Cannot Be Called with Ascopy = No on Non-Main Thread

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

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

DispatchQueue doesn't behave like serial queue

DispatchQueue(label: "be.io") will create a new instance every time you call it

To get as you expect make it an instance var like

let mySerialQueue = DispatchQueue(label: "be.io") 

Why calling a dialog with a struct drops this exception?

The error message give a big clue:

Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Modifications to the
layout engine must not be performed from a background thread after it
has been accessed from the main thread.' terminating with uncaught
exception of type NSException

The URLRequest run asynchronously on a background thread, including its completion handlers. You (generally) can't do UI work outside of the main thread. To display the results you need to push the operation back onto the main thread:

DispatchQueue.main.async {
// do UI work
}

DispatchQueue main isn't called

I assume this code is in the main thread.

If so, you have sync'd to a background thread and from there tried to sync back to DispatchQueue.main. But that queue is waiting for the background one to return before it can do anything.



Related Topics



Leave a reply



Submit