Alamofire http json请求块ui

巨人克伦克

我一直在创建一个从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()

        }
        }

}
斯蒂芬·萨拉蒂奇(Stefan Salatic)

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

在这种情况下,您应该将其dispatch_async用于JSON解析,并且仅在完成后更新主线程。

像这样做你的解析

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章