等到alamofire完成获取请求并创建对象

编程入门 行业动态 更新时间:2024-10-11 19:15:38
本文介绍了等到alamofire完成获取请求并创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在等待创建一个对象时遇到问题,然后希望更新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完成获取请求并创建对象

本文发布于:2023-11-26 02:03:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!