Wait until alamofire is done getting request and making object [duplicate]

╄→гoц情女王★ 提交于 2019-12-11 04:07:53

问题


I am having a problem with waiting until one object is made, then I wish to update my UI. If i run the function, it will instantly get the out of index error because its trying to get some text from the first object when it dosent exist yet... Anyone who could help me?

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:


Use completion block/closure.

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

}

Finally call this method,

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

Why?

Because Alamofire responseJSON is an asynchronous call. When you call your method getMovieData obviously it returns without waiting for responseJSON to finish. You need a completion handler to inform the VC which called getMovieData to inform the completion of it.

Because Alamofire responseJSON block is triggered on main thread, you can directly update the UI in the completion block without having to change the thread.



来源:https://stackoverflow.com/questions/43204249/wait-until-alamofire-is-done-getting-request-and-making-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!