Jordi Gámez
Jordi Gámez

Reputation: 3544

Using completionHandler in Alamofire Swift 2.2

Good morning,

I'm trying to use completionHandler with Alamofire in Swift 2.2 for the first time and I'm a little bit lost. In my example, I'm trying to make 3 API calls (trakt.tv API) but I'm not doing it correctly because there are some missing values due to the completionHandler.

My question is: How can I tell my function (with a completionHandler) to wait until the other 2 functions (getOverview and getPicture) are executed? I tried using another completionHandler in both functions but it didn't work.

That's my function:

func getMovies(url: String, clientID: String, completion : ([Movie]) -> ()) {

        let headers = ["trakt-api-version":"2", "Content-Type": "application/json", "trakt-api-key": clientID]

        Alamofire.request(.GET, url, headers: headers).responseJSON { response in

            if response.result.isSuccess {
                let movieInfo = JSON(data: response.data!)

                for result in movieInfo.arrayValue {

                    let slug = result["ids"]["slug"].stringValue
                    let title = result["title"].stringValue
                    let year = result["year"].stringValue

                    // OVERVIEW
                    self.getOverview(slug, clientID: clientID) { response in
                        print("Overview")
                        print(self.overview)
                    }

                    // PICTURE
                    self.getPicture(slug, clientID: clientID) { response in
                        print("Picture")
                        print(self.picture)
                    }

                    let movie = Movie(slug: slug, title: title, year: year, overview: self.overview, picture: self.picture)

                    print("Slug: "+slug)
                    print("Title: "+title)
                    print("Year: "+year)
                    // EMPTY
                    print("Overview: "+self.overview)
                    // EMPTY
                    print("Picture: "+self.picture)

                    self.movies.append(movie)
                }
                completion(self.movies)
            } else {
                print(response.result.error)
            }
        }
    }

That's my call:

getMovies(url, clientID: self.clientID) { response in
            print(self.movies)
            self.tableView.reloadData()
        }

And that's my getOverview function:

func getOverview(slug: String, clientID: String, completion : (String) -> ()) {

    let movieURL: String = "https://api.trakt.tv/movies/"+slug+"?extended=full"

    let headers = ["trakt-api-version":"2", "Content-Type": "application/json", "trakt-api-key": clientID]

    Alamofire.request(.GET, movieURL, headers: headers).responseJSON { response in

        if response.result.isSuccess {
            let movieInfo = JSON(data: response.data!)
            self.overview = movieInfo["overview"].stringValue
            completion(self.overview)
        } else {
            print(response.result.error)
        }
    }
}

Regards

Upvotes: 0

Views: 128

Answers (1)

Welton122
Welton122

Reputation: 1131

I would use Dispatch Groups to solve this issue. Using these you are able to wait until a process or processes having completed (with a timeout). Here is a link to a post with further details.

http://commandshift.co.uk/blog/2014/03/19/using-dispatch-groups-to-wait-for-multiple-web-services/

Upvotes: 1

Related Questions