iOS 13: @objc VC.navigationController(_:willShow:animated:) : threading violation: expected the main thread
I got a solution for the above issue,
I used Xcode 10.2.1 to upload the build of the app, and I didn't get any crashes now.
Multithreaded rendering only crashes on iOS 13
As Gene said, the issue is that previous versions of iOS wouldn’t stop when you incorrectly interacted with the UI from a background thread. iOS 13 will.
To help identifying the issue, go to “Editor” » “Scheme” » “Edit Scheme...” and then check “Main Thread Checker”. If you tap on the right arrow next to this, it will even create a breakpoint for issues, where it will stop execution at the line at which you are trying to update the UI from a background 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.
Are SKProductsRequestDelegate methods always called on the main thread?
I confirmed with iOS 13.1.2 that they can indeed be called from a different queue: com.apple.root.default-qos (concurrent)
. Eek.
The solution is to dispatch that work back to the main thread via DispatchQueue.main.async
.
iOS: Hide the navigation bar for only one viewcontroller which is root of the UINavigationController?
I got the another way to hide/show navigationbar from one of my friend.
- Set a delegate for the NavigationController:
navigationController.delegate = self
- Hide/Show navigationbar for each ViewController all in one place
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let hide = (viewController is YourVC)
navigationController.setNavigationBarHidden(hide, animated: animated)
}
Main Thread Checker warning with CoreMotion, only appearing on 2018 model iPhones
New answer when using iOS 13
I have tested this just now with the newly released iOS 13 version (17A577) on a 2018 device and it no longer produces the threading error.
Previous answer when using iOS 12
This is a known issue that has been reported to Apple with a radar linked here.Related Topics
Monitoring App Switching on Os X
Split Uint32 into [Uint8] in Swift
Swift Task Continuation Misuse: Leaked Its Continuation - for Delegate
How to Implement Protocol Methods That Return Covariant Selfs
Why Is Calendar.Date(From: Datecomponents) Adding Time
How Are Hash Collisions Handled
How to Generate a Binding for Each Array Element
Why Is the Alert Triggering Out of Order If Value Is Not Updated
How to Trigger an Action When a Swiftui Toggle() Is Toggled
Setting Backgroundcolor of Custom Nsview
Programmatically Disabling Screenshot in App
Function That Takes a Protocol and a Conforming Class (!) Instance as Parameters
Why Do Integers Not Conform to the Anyobject Protocol
Using Uiapplicationdelegateadaptor to Get Callbacks from Userdidacceptcloudkitsharewith Not Working
Naming Convention for Optional Binding
Swift Switch Statement Considered All Cases of Int, But Compiler Still Display Error
Opposite of _Conversion in Swift to Assign to a Value of a Different Type
How to Distinguish Between Multiple Uipickerviews on One Page