Alamofire http json 请求块 ui

编程入门 行业动态 更新时间:2024-10-12 05:53:04
本文介绍了Alamofire http json 请求块 ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在创建一个从 JSON 脚本中检索对象的函数.为此,我选择使用 alamofire 进行异步请求,使用 swiftyJSON 进行轻松解析.但是我似乎对它阻止 UI 有问题?当它是异步请求时怎么会这样?我是否需要在单独的线程上运行它或者可能的解释是什么?

I've been creating a function which retrieve objects from a JSON script. I've chosen for this to use alamofire for async request and swiftyJSON for easy parsing. However i seem to have a problem with it blocking the UI? How come it does that when it is async request? Do i need to run it on a separate thread or what could the explanation be?

基本上我所说的阻塞 UI 的意思是在下面的函数执行完成之前它不会对其他按钮做出反应.

Basically what i mean by blocking UI is that it does not react on other buttons before the below function is finished executing.

func getRecent() { var url = "URL/recent.php?lastid=(lastObjectIndex)&limit=(numberOfRecordsPerAPICall)" isApiCalling = true request(.GET, url, parameters: nil) .response { (request, response, data, error) in if error == nil { let data: AnyObject = data! let jsonArray = JSON(data: data as! NSData) if jsonArray.count < self.numberOfRecordsPerAPICall { self.recentCount = 0 self.tableVIew.tableFooterView = nil } else { self.recentCount = jsonArray.count self.tableVIew.tableFooterView = self.footerView } for (key: String, subJson: JSON) in jsonArray { // Create an object and parse your JSON one by one to append it to your array var httpUrl = subJson["image_url"].stringValue let url = NSURL(string: httpUrl) let data = NSData(contentsOfURL: url!) if UIImage(data: data!) != nil { // Create an object and parse your JSON one by one to append it to your array var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: UIImage(data: data!)!, summary: subJson["news_text"].stringValue, date: self.getDate(subJson["date"].stringValue)) self.recentArray.append(newNewsObject) } } self.lastObjectIndex = self.lastObjectIndex + self.numberOfRecordsPerAPICall self.isApiCalling = false self.tableVIew.reloadData() self.refreshControl?.endRefreshing() } } }

推荐答案

响应闭包在主线程上执行.如果您在那里进行 JSON 解析(并且您有大量数据),它将阻塞主线程一段时间.

The response closure is executed on the main thread. If you are doing your JSON parsing there (and you have a large amount of data) it will block the main thread for a while.

在这种情况下,您应该使用 dispatch_async 进行 JSON 解析,并且只有在您完成后才更新主线程.

In that case, you should use dispatch_async for the JSON parsing and only when you are completed update the main thread.

像这样进行解析

func getRecent() { var url = "URL/recent.php?lastid=(lastObjectIndex)&limit=(numberOfRecordsPerAPICall)" isApiCalling = true request(.GET, url, parameters: nil) .response { (request, response, data, error) in if error == nil { let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(priority, 0)) { // Parse stuff here let data: AnyObject = data! let jsonArray = JSON(data: data as! NSData) if jsonArray.count < self.numberOfRecordsPerAPICall { self.recentCount = 0 self.tableVIew.tableFooterView = nil } else { self.recentCount = jsonArray.count self.tableVIew.tableFooterView = self.footerView } for (key: String, subJson: JSON) in jsonArray { // Create an object and parse your JSON one by one to append it to your array var httpUrl = subJson["image_url"].stringValue let url = NSURL(string: httpUrl) let data = NSData(contentsOfURL: url!) if UIImage(data: data!) != nil { // Create an object and parse your JSON one by one to append it to your array var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: UIImage(data: data!)!, summary: subJson["news_text"].stringValue, date: self.getDate(subJson["date"].stringValue)) self.recentArray.append(newNewsObject) } } dispatch_async(dispatch_get_main_queue()) { // Update your UI here self.lastObjectIndex = self.lastObjectIndex + self.numberOfRecordsPerAPICall self.isApiCalling = false self.tableVIew.reloadData() self.refreshControl?.endRefreshing() } } } } }

更多推荐

Alamofire http json 请求块 ui

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

发布评论

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

>www.elefans.com

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