How do you handle derived state

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:

  1. Your text input produces a stream of String
  2. Map that to a stream of URL (validate, print errors if the format is wrong, etc.)
  3. flatMapLatest that with a func 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
  1. Bind the stream of images to your image view