iOS 13: Threading Violation: Expected the Main Thread

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:

Sample Image

*** 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



Leave a reply



Submit