I have a SwiftUI view where I display an image, which is taken from MapKit's MKMapSnapshotter, that relies on some information that is derived from a Core Data property (namely, the coordinates and the color of the marker). I want to be able to regenerate the image whenever any of that information changes. I can't figure out how to accomplish this. It's not as simple as using an @ObservedObject for the NSManagedObject that I want to observe, since there attributes that can change aren't visible directly on the SwiftUI view, which is showing just an image.
I tried use an onChange on some of the Core Data attributes but it won't fire (probably because the attributes aren't actually visible on the SwiftUI view). Is there any other solution?
struct CJContactProfileAddressLabelTestView: View {
@ObservedObject var personAddress: PersonAddress
@ObservedObject private var locationManager: ContactProfileLocationManager // generates an image using MKMapSnapshotter
init(personAddress: PersonAddress) {
self.personAddress = personAddress
self.locationManager = ContactProfileLocationManager(personAddress: personAddress)
}
var body: some View {
VStack {
if let addressString = personAddress.addressStringWithoutCountry() {
Text("\(addressString)").multilineTextAlignment(.leading)
}
if let mapImage = locationManager.mapImage {
Image(uiImage: mapImage)
.resizable()
.cornerRadius(10)
}
}
.task {
print("CJProfileAddressView: task called")
await self.locationManager.getMapSnapshot(from: CLLocation(latitude: self.personAddress.latitude as! CLLocationDegrees, longitude: self.personAddress.longitude as! CLLocationDegrees))
}
}
}
@StateObject var locationManager.... Also you are not not creating yourself.locationManagerininit(...)correctly, it should beself._locationManager = StateObject(wrappedValue: ContactProfileLocationManager(personAddress: personAddress))See this SO post for examples, alternatives and explanations: stackoverflow.com/questions/62635914/…