【问题标题】:Using completionHandler in Alamofire Swift 2.2在 Alamofire Swift 2.2 中使用 completionHandler
【发布时间】:2016-08-01 06:46:22
【问题描述】:

早上好,

我第一次尝试在 Swift 2.2 中将 completionHandler 与 Alamofire 一起使用,但我有点迷茫。在我的示例中,我正在尝试进行 3 个 API 调用(trakt.tv API),但我没有正确执行,因为由于 completionHandler 存在一些缺失值。

我的问题是:如何告诉我的函数(使用 completionHandler)等到其他 2 个函数(getOverview 和 getPicture)被执行?我尝试在这两个函数中使用另一个 completionHandler,但它不起作用。

这是我的功能:

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)
            }
        }
    }

这是我的电话:

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

这就是我的 getOverview 函数:

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)
        }
    }
}

问候

【问题讨论】:

    标签: swift swift2 alamofire completionhandler


    【解决方案1】:

    我会使用调度组来解决这个问题。使用这些,您可以等到一个或多个进程完成(超时)。这是一个帖子的链接,其中包含更多详细信息。

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

    【讨论】:

    • 嗨@Welton122,我正在尝试使用调度组更改我的代码,但由于我从未使用过它们,我遇到了很多问题。你能给我一点光吗?我正在尝试遵循 Stackoverflow 的一些教程和其他练习,但我无法做到正确。问候。
    • 感谢@Welton122,我使用了 Dispatch Groups,终于成功了。
    猜你喜欢
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多