user844288
user844288

Reputation: 27

How do I return a UIView in a Koloda method when waiting for image from API?

I am attempting to create a Tinder for Movies app where the movies are loaded in using The Movie Database API and I am using Koloda pod for the card swiping features. I have run into a problem where I have to implement a Koloda datasource method where I have to return a UIView (which will be presented on the card).

The issue is that I would like to retrieve the image from the database and it might make the app slow if I were to load all the images beforehand, so I would like to retrieve each image when needed. This causes an error as retrieving the image is asynchronous and despite using completion handlers, I run into an issue where I cannot return a value from within a closure, as it still requires a return value outside the closure. But if I were to provide another return value outside the closure, this return statement always gets triggered and I cannot return the image I want to retrieve. Meaning I will always return the noImageAvailable image. May I know how to solve this issue? Thanks!

    func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
    
    let movie = movies[index]
    let poster = movie.posterImage
    var imageView = UIImageView(image: UIImage(named: "noImageAvailable"))
    
    guard let posterString = poster else {
        return imageView
    }
    
    let urlString = "https://image.tmdb.org/t/p/w300" + posterString
    
    guard let posterImageURL = URL(string: urlString) else {
        return imageView
    }
    
    
    getImageDataFrom(url: posterImageURL) { image in
        DispatchQueue.main.async {
            imageView = UIImageView(image: image)

        }
    }
    
    return imageView
    
}


private func getImageDataFrom(url: URL, handler: @escaping (UIImage) -> Void) {
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        // Handle Error
        if let error = error {
            print("DataTask error: \(error.localizedDescription)")
            return
        }
        
        guard let data = data else {
            // Handle Empty Data
            print("Empty Data")
            return
        }
        
        if let image = UIImage(data: data) {
            handler(image)
        } else {
            handler(UIImage(named: "noImageAvailable")!)
        }
    }.resume()
}

Upvotes: 0

Views: 116

Answers (0)

Related Questions