我一直在创建一个从JSON脚本中检索对象的函数。我选择使用alamofire进行异步请求,使用swiftyJSON进行简单的解析。但是我似乎有阻止UI的问题?为什么它是异步请求呢?我是否需要在单独的线程上运行它或者解释是什么?
基本上我的意思是阻止UI是在下面的函数执行完毕之前它不会对其他按钮做出反应。
func getRecent() {
var url = "http://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()
}
}
}
答案 0 :(得分:4)
响应闭包在主线程上执行。如果您正在那里进行JSON解析(并且您有大量数据),它将阻塞主线程一段时间。
在这种情况下,您应该使用dispatch_async
进行JSON解析,并且仅在完成更新主线程时使用。{/ p>
只需像这样进行解析
func getRecent() {
var url = "http://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()
}
}
}
}
}
答案 1 :(得分:0)
Swift5 更新Stefan Salatic的答案,如果您要从Alamofire响应中解析大量json数据,则最好使用全局调度队列,并且如果出于任何原因需要将主线程中的UI更新为DispatchQueue.main .async。
因此示例代码如下所示。
AF.request(UrlGetLayers, method: .post, parameters: parameters, headers: headers)
.responseJSON { response in
DispatchQueue.global(qos: .background).async {
//parse your json response here
//oops... we need to update the main thread again after parsing json
DispatchQueue.main.async {
}
}
}