*** 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
Access Class Property from Instance
Errors Encountered While Discovering Extensions: Error Domain=Pluginkit Code=13 "Query Cancelled"
Protocol Associated Type Typealias Assignment Compile Error
Bad_Access During Recursive Calls in Swift
Produce Sounds of Different Frequencies in Swift
Why Is The Leading Swipe Action Also Duplicated as a Trailing Action
Get Output Frames Failed, State 8196
Testing a Timer in Xcode with Xctest
Using UIdropinteractiondelegate and Movies
My Structure Does Not Conform to Protocol 'Decodable'/'Encodable'
When How to Start Submitting Apps to The iOS App Store Written Using The Swift Programming Language
Cannot Assign to Value: 'self' Is Immutable
Sknode Subclass Generates Error: Cannot Invoke Initializer for Type "X" with No Arguments
Scrolltoitem at Indexpath at .Top Hides Cell Under Header When Sectionheaderspintovisiblebounds
How to Assign a Generic Function to a Variable