1

We are using AlamofireImage in our project to make use of its image caching system. However, I'm wondering the best way to fetch the image without contradicting MVVM.

Alamofire's image download method returns the following type: AFIDataResponse<Image>.

Therefore the obvious way to implement is via the following:

func fetchImage() {
        guard let imageURL = viewModel.imageURL else { return }
        let urlRequest = URLRequest(url: imageURL)

        imageDownloader.download(urlRequest, completion: { response in
            if case .success(let image) = response.result {
                self.productImage = image
            }
        })
    }

However, this means including this logic within the view itself, which is not ideal. Particularly given that I would like to also add an isLoading publisher to present a loading view when the image is downloading.

However, if I move this logic into the viewModel, we then have to declare productImage in said viewModel and import UIKit, which again is not ideal. Both work, but neither seems to stick correctly to MVVM.

DevB1
  • 1,235
  • 3
  • 17
  • 43
  • 1
    Sometimes it's just not practical to force sticking to architecture patterns down to every last subview. What may be a good solution is to build something similar to the SwiftUI native `AsyncImage`. That way, yes, your `CustomAsyncImage` (or whatever) violates MVVM, but it will do that to prevent spreading violations everywhere else where you need to download and display images. In a very locally contained scope breaking an architecture may not be super bad. In the end Apple does not use MVVM internally in their views either. Is that bad? Not really. – JanMensch Mar 07 '23 at 13:32

0 Answers0