*** Terminating App Due to Uncaught Exception 'Nsinternalinconsistencyexception', Reason: 'Threading Violation: Expected The Main Thread'

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'threading violation: expected the main thread'

The error tells you want to do:

reason: 'threading violation: expected the main thread'

Many times, when you pass a closure, you could be called back on a background thread. But, if you want to do anything with the UI, that must be done on the main thread. We use DispatchQueue.main.async to pass a block to the main queue and have it run on the main thread asynchronously,

In

PHPhotoLibrary.requestAuthorization({ (newStatus) in
if newStatus == PHAuthorizationStatus.authorized {
self.showPhotoActionSheet()
}
})

You need to dispatch to the main thread

PHPhotoLibrary.requestAuthorization({ (newStatus) in
if newStatus == PHAuthorizationStatus.authorized {
DispatchQueue.main.async {
self.showPhotoActionSheet()
}

}
})

It looks like you might be calling checkPermission on the background. If so, you also need to wrap the call to:

self.showPhotoActionSheet()

From the call stack -- it looks like this might be the one you are having problems with.

NSInternalInconsistencyException', reason: 'Only run on the main thread!' error

I'm guessing that the callback from AJAXUtils happens on a background thread. I don't know if that is your code or not but you should do all UI operations (like setting text in a label, setting an image in an image view, pushing a view controller) on the main thread.

dispatch_sync(dispatch_get_main_queue(), ^{
/* Do UI work here */
});


Related Topics



Leave a reply



Submit