等到alamofire完成获取请求并制作对象

时间:2017-04-04 10:11:01

标签: ios swift alamofire

我遇到了等待一个对象的问题,然后我希望更新我的UI。如果我运行该函数,它将立即获得索引错误,因为它试图从第一个对象获取一些文本时它还没有...任何可以帮助我的人?

func getMovieData(activeGenreLink: String){

    //self.posterLoading.startAnimating()

        Alamofire.request(activeGenreLink).responseJSON { response in
            //print(response.request)  // original URL request
            //print(response.response) // HTTP URL response
            //print(response.data)     // server data
            //print(response.result)   // result of response serialization
            var test = 0
            self.movieArray = []


            if let json = response.result.value as? Dictionary<String,AnyObject> {
                if let movies = json["results"] as? [AnyObject]{
                    for movie in movies{
                        let movieObject: Movie = Movie()

                        let title = movie["title"] as? String
                        let releaseDate = movie["release_date"] as! String
                        let posterPath = movie["poster_path"] as! String
                        let overView = movie["overview"] as! String
                        let movieId = movie["id"] as! Int
                        let genre_ids = movie["genre_ids"] as! [AnyObject]

                        movieObject.title = title
                        movieObject.movieRelease = releaseDate
                        movieObject.posterPath = posterPath
                        movieObject.overView = overView
                        movieObject.movieId = movieId


                        for genre in genre_ids{//Genre ids, fix this
                            movieObject.movieGenre.append(genre as! Int)
                        }



                        self.movieArray.append(movieObject)


                    }//End of for each movie
                }

            }


    }//End of Json request



}//End of getmoviedata

1 个答案:

答案 0 :(得分:3)

使用完成块/闭包。

func getMovieData(activeGenreLink: String,completion : ()->()){

    //self.posterLoading.startAnimating()

        Alamofire.request(activeGenreLink).responseJSON { response in
            //print(response.request)  // original URL request
            //print(response.response) // HTTP URL response
            //print(response.data)     // server data
            //print(response.result)   // result of response serialization
            var test = 0
            self.movieArray = []


            if let json = response.result.value as? Dictionary<String,AnyObject> {
                if let movies = json["results"] as? [AnyObject]{
                    for movie in movies{
                        let movieObject: Movie = Movie()

                        let title = movie["title"] as? String
                        let releaseDate = movie["release_date"] as! String
                        let posterPath = movie["poster_path"] as! String
                        let overView = movie["overview"] as! String
                        let movieId = movie["id"] as! Int
                        let genre_ids = movie["genre_ids"] as! [AnyObject]

                        movieObject.title = title
                        movieObject.movieRelease = releaseDate
                        movieObject.posterPath = posterPath
                        movieObject.overView = overView
                        movieObject.movieId = movieId


                        for genre in genre_ids{//Genre ids, fix this
                            movieObject.movieGenre.append(genre as! Int)
                        }



                        self.movieArray.append(movieObject)


                    }//End of for each movie
                }

            }

           completion()
    }//End of Json request

}

最后调用此方法,

self.getMovieData(activeGenreLink: "your_link_here", completion: {
            //updateUI
})

<强>为什么吗

因为Alamofire responseJSON是异步调用。当您调用方法getMovieData时,它会返回而不等待responseJSON完成。您需要一个完成处理程序来通知调用getMovieData的VC以通知其完成。

因为在主线程上触发了Alamofire responseJSON块,所以您可以直接更新完成块中的UI,而无需更改线程。