I have a background operation (a web fetch) that grabs some data from the internet. I need to dispatch out that data as soon as possible during the fetch and have been using the following:
func dispatchWebData(myNumber: Double) {
if UIApplication.shared.applicationState == .active {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.myFunction(myNumber: myNumber)
}
} else {
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now() + 0.2) {
self.myFunction(myNumber: myNumber)
}
}
}
If the app is active, data handling is dispatched to the Main thread and the UI gets updated as soon as the data is available. If the app is not active, the main thread is not available so I dispatched data handling to the global thread. This was/is working fine.
Now the MainThread Checker is sending me the little purple square warning that "UIApplication.applicationState must be used from main thread only".
First, it seems silly that you can't check the application state from the background. What's the point if you can only check the state while the state is Active?
But I wouldn't care about that issue as long as I can get my data handling to move forward. So I'm wondering is if I can check for the existence of the main thread and then dispatch to it if it's there, otherwise to the global thread. Other ideas welcome.