If I have a view with a derived state, how do I handle resetting it when the values of the view change? For instance, if I have a "URLImage" that downloads an image based on a URL input, when the url is changed by the parent view, I would need to clear out the downloaded image. @State seems to stick around even as the normal properties get changed.
1 Like
I've only worked with Rx, and not SwiftUI, but the general approach is such:
- Your text input produces a stream of
String
- Map that to a stream of
URL
(validate, print errors if the format is wrong, etc.) -
flatMapLatest
that with afunc downloadImage(url: URL) -> Observable<UIImage>
, to produce a stream of images
- When your image download finishes, it'll be emitted and forwarded to your image view
- If you change the URL before the download finishes, the old download would be ignored, and your new one would be used when it's ready
- Bind the stream of images to your image view