Main Thread Checker: Ui API Called on a Background Thread: -[Uiapplication Applicationstate]

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]

First, make sure your invocations of google maps and ui changes are called from the main thread.

You can enable Thread Sanitizer option in Xcode using below steps:


You can put offending lines on the main thread with the following:

DispatchQueue.main.async {
//Do UI Code here.
//Call Google maps methods.

Also, update your current version of google maps. Google maps had to make a couple of updates for the thread checker.

For the question: "Why would this be occurring?" I think Apple added an assertion for an edge case which Google then had to update their pod for.

UI API called on a background thread error

As you see from the error, you can't call UIApplication.shared from a background thread. Since you don't want to have to wrap every call to our getContext method in DispatchQueue.main.async, you can update your getContext method to do the necessary wrapping as needed:

private class func getContext() -> NSManagedObjectContext {
let appDelegate: AppDelegate
if Thread.current.isMainThread {
appDelegate = UIApplication.shared.delegate as! AppDelegate
} else {
appDelegate = DispatchQueue.main.sync {
return UIApplication.shared.delegate as! AppDelegate
return appDelegate.weatherPersistentContainer.viewContext

This code ensures that UIApplication.shared is only called on the main queue, even if getContext is called from a background thread. The nice part is that the result is returned on the original thread.

Solve Main Thread Checker: UI API called on a background thread: -[UIApplication setNetworkActivityIndicatorVisible:] in UITests

It usually goes away when I simply

1. run the app normally in the simulator

2. Try running my XCUITest again.

Related Topics

Leave a reply