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

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)

    }

}
    
asked by anonymous 16.07.2016 / 06:28

1 answer

0

Actually, you can not make findProfessions() wait for the asynchronous response of Alamofire to return the function.

I suggest you make the function return in Void and pass closure to the method just like you did in   getNetworkImage() :

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