Alamofire http json请求块ui

我一直在创build一个从JSON脚本中检索对象的函数。 我select这个使用alamofire作为asynchronous请求和swiftyJSON以便于parsing。 不过,我似乎有一个问题,阻止用户界面? 当它是asynchronous请求时,它怎么做? 我是否需要在单独的线程上运行它,或者解释是什么?

基本上我的意思是阻止用户界面,它是不会在其他button下面的function完成执行之前作出反应。

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() } } } 

响应闭包在主线程上执行。 如果你在那里做你的JSONparsing(并且你有大量的数据),它会阻塞主线程一段时间。

在这种情况下,您应该使用dispatch_async进行JSONparsing,并且只有在完成更新主线程时才使用。

只要做这样的parsing

 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() } } } } }