Has anyone figured out how and when SwiftUI bindings trigger view updates?
Here's an example I fail to understand. In this example, ParkingStore
is a BindableObject
that fetches data from a server and sends a didChange
event every 5 seconds. When this happens, the map should update and add annotations for new data received from the server.
import MapKit
import SwiftUI
struct MapView: UIViewRepresentable {
@ObjectBinding var store: ParkingStore
// class RandomClass { }
// let x = RandomClass()
func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator()
}
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
// Create and return a map
}
func updateUIView(_ view: MKMapView, context: UIViewRepresentableContext<MapView>) {
// Adjust map annotations using the ParkingStore
}
}
When this view loads, it calls makeCoordinator
, then makeUIView
, then updateUIView
twice. However, this happens immediately and before store
has any data or sends any didChange
event. Afterwards, updateUIView
is never called.
Strangely, when I add a stored property such as the one currently commented out, updateUIView
does get called every time store
sends a didChange
event. This only seems to work if the property is of a reference type.
Is this a bug, or am I missing something?