I'm trying to figure out the right approach to wait for location data to become available, before adding/drawing subview/sublayers without blocking the main thread. E.g. I want to draw views based on long/lat of current location of current user that authorized such.
I have a CLLocationManager related object that does the configuration and waits for a callback from the OS with long/lat.
I want to display something in a UIViewController (that is one of the main tab bar items), but only after the location becomes available and my location function gets the callback from iOS.
But it seems to be a Catch-22. If I try waiting on the main thread, of course it blocks everything, but if I wait for the data on a background thread, I can't tamper with UIViews from that thread.
How can this be handled?
CodePudding user response:
When you request location information (e.g., startUpdatingLocation
), you may not receive location information immediately. So you need to decide what you want to show in your UI in the intervening period of time. A blank/blurring view with a spinner (e.g., UIActivityIndicatorView
) on top of it? Maybe save the last known location in persistent storage so that when the user fires up the app again, they at least see information relevant to the last known position until new location information comes in?
But you never block the main thread (which will only freeze the app, leading the user to suspect that the app died, and you risk having the watchdog process kill your app). Just decide what UX you want until the location information is retrieved, and present that until the new, updated locations start coming in.