Xcode 11, iOS 13 - NSInternalInconsistencyException

App is crashing when presenting an UIAlertController inside a background thread(Inside URLSession dataTaskWithRequest completion handler).

Error log says"Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread."

This happens when App is built with Xcode 11.0 and running on iOS 13 devices. Otherwise works fine with all the previous Xcode versions.

Replies

You are calling UIKit from a secondary thread. This is simply not supported. You’ll need to bounce to the main thread before touching UIKit.

Also, if you’re using a modern version of Xcode you can enable the main thread checker (in the Diagnostics tab of the scheme editor) to catch such problems in day-to-day debugging.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Xcode 11, iOS 13 - NSInternalInconsistencyException

Thank you for the Reply!


How does it work on Apps built with lower Xcode versions? Is it made a mandatory check from Xcode 11.0, to not call UIKit from a secondary thread?

How does it work on Apps built with lower Xcode versions?

I’m not entirely sure. Calling UIKit from a secondary thread results in undefined behaviour [1]. Under some circumstances it works (or seems to work), in others it just crashes, in others it’s detected and trapped. This behaviour can vary from call to call, run to run, and release to release. You’ll also find that some behaviour changes based on a linker-on-or-later check, which is what I suspect is happening here.

Is it made a mandatory check from Xcode 11.0, to not call UIKit from a secondary thread?

It has always been illegal to call UIKit from a secondary thread, it’s just that both the OS and the tools have got better about detecting such bugs.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] With a few exceptions, typically revolving around images and drawing. There used to be a document that summarises this change (broken URL) but that info has now been integrated into the main UIKit documentation. If you’re curious, you can find a copy of The Wayback Machine.