iosswiftnsurlsessioncompletionhandlerkoloda

Return in Completion Block Swift


I'm implementing the KolodaView available at: https://github.com/Yalantis/Koloda. The viewForCardAt function returns a UIView, and my UIView will have an image which needs to be downloaded. The problem is that the function itself expects a return type of UIView but I have no way of knowing when the completion block of the setupCard method is done executing, therefore I might end up returning an empty FlatCard instead of the FlatCard obtained in the completion block. I tried adding return a to the completion block, but this isn't allowed. How can I change the code below to guarantee that a card is only returned once the completion block is executed.

func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {

    var a = FlatCard()
    if let listings = all_listings {
        if index < listings.count {
            setupCard(index: index, listings: listings, { (complete, card) in
                if (complete) {
                    a = card
                }
            })
            return a
        }
     }
    return a
}

func setupCard(index: Int, listings : [Listing], _ completionHandler: @escaping (_ complete: Bool, _ card : FlatCard) -> ()) -> (){

    let curr_card = FlatCard()

    if let main_photo_url = listings[index].pic1url {
        URLSession.shared.dataTask(with: main_photo_url, completionHandler: { (data, response, error) in

            if (error != nil) {
                print(error)
                return
            }

            DispatchQueue.main.async {
                curr_card.mainFlatImage = UIImage(data: data!)
            }
        })
        completionHandler(true,curr_card)
        return
    } else {
        completionHandler(true,curr_card)
        return
    }
}

Solution

  • You cannot return something before it is ready.

    Personally, I would update FlatCard so that it can download the image itself and update it's own view when done.

    Something along the lines of

    class FlatView: UIView {
    
        var imageURL: URL? {
            didSet {
                if let imageURL = newValue {
                     // download image, if success set the image on the imageView
                }
            }
        }
    }
    

    Then all that you need to do in your other function is...

    func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
    
        var a = FlatCard()
        if let listings = all_listings {
            if index < listings.count {
                a.imageURL = listings[index].pic1url
            }
         }
        return a
    }