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
What Is an Optional Value in Swift
Failing Cast in Swift from Any? to Protocol
Why Constant Constraints the Property from a Structure Instance But Not the Class Instance
Cannot Assign Property in Method of Struct
How to Encode a String to Base64 in Swift
Swift: How to Use Preprocessor Flags (Like '#If Debug') to Implement API Keys
What Is _: in Swift Telling Me
Detect When a Tab Bar Item Is Pressed
Using a Type Variable in a Generic
Call a Method from a String in Swift
Get Integer Value from String in Swift
What Does a Module Mean in Swift
How to Find String B Missing Characters Based on String a and Add Them to String B
How to Set Cell Spacing and Uicollectionview - Uicollectionviewflowlayout Size Ratio