How to do a requisicao using alamofire and make the following lines of code wait for the reply of the request?

Asked

Viewed 130 times

0

    class func findProfessions() -> [PhotoProfession] {      

    var professions = [PhotoProfession]()

    let backendless = Backendless.sharedInstance()

    if let p = backendless.data.of(Profession.ofClass()).find() {

        for prof in p.data {

            let photo = PhotoProfession(title: (prof as! Profession).name!)
            let pathImage = (prof as! Profession).pathIcon!

            _ = PhotoProfession.getNetworkImage(pathImage) { image in
                photo.image = image!.decompressedImage
            }                

            professions.append(photo)

        }
    }
    return professions
}

class func getNetworkImage(urlString: String, completion: (UIImage? -> Void)) -> (Request) {        

    return Alamofire.request(.GET, urlString).response {
        (_, _, data, _) in

        let image = UIImage(data: data! as NSData)
        completion(image)

    }

}

1 answer

0

In fact you can’t make findProfessions() wait for the answer asynchronous of the Alamofire to then make the return of the function.

I suggest you turn the return function into Void and spend a closure for the method just as you did in the getNetworkImage():

class func findProfessions(callback: [PhotoProfession] -> Void) { ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.