I have an API that spits out some GPS data to a GET endpoint.
I created a decodable struct that holds this information, and have a method that decodes the information correctly.
I have a class of type @ObservableObject that has an @Published property of this struct type. I set an instance of this class as an environmentObject in my @main view.
This looks like this
I am really struggling with binding this information to a mapview in the application. My initial hack was to call the 'getMostRecentLocation()' method when the Map() view is displayed... then update an @state property in that view. But as that call happens all async and whatnot... the method wasn't updating the view.
I did work around this with a hacky little button that updates the view, then re-calls the method.... so we are always one call behind. This is setting the location to the data gathered when the view loaded. If I hit the button again, it will show the data loaded when the button was pressed this time etc... which is pretty trash. I'd like the view to update automatically if that EnvironmentObject updates, so I can add code to poll the API in the background and wanted the map to 'bind' to that information so that the map will follow the updated data. That view looks like this now.
EDIT: I've removed some unneeded code, and just put it all here for clarity.
I can't seem to use the environment object information for the map as it expects Binding<> objects/properties. Any help here would be great. I must just be using some wrong pattern or something.
SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.