问题描述
我在等待创建一个对象时遇到问题,然后希望更新UI。如果我运行该函数,它将立即退出索引错误,因为当它存在时,它试图从第一个对象中获取一些文本...有人能帮助我吗?
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
推荐答案
使用完成块/结束符。
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完成。您需要一个完成处理程序来通知VC,VC调用了 getMovieData
来通知其完成。
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.
由于Alamofire responseJSON块在主线程上触发,因此您可以直接在完成块中更新UI,而无需更改线程。
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.
这篇关于等到alamofire完成获取请求并创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!