【问题标题】:swift tableview displays json data very slowswift tableview显示json数据很慢
【发布时间】:2015-08-17 23:15:10
【问题描述】:

我有一个循环 JSON 数据并显示在表格视图中的页面。 我得到了正确的结果,但是当它显示在页面上时,速度很慢。 我尝试打印 json 以查看它检索 json 数据的速度有多快,而且速度非常快。 还有一件很奇怪的事情是,当它加载时,如果我拖动页面,一切都会立即显示出来。 下面是我放在 viewdidload 函数中的代码

        self.PoTable.separatorStyle = UITableViewCellSeparatorStyle(rawValue: 0)!
        self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
        PoTable.delegate = self
        PoTable.dataSource = self
        self.PoTable.addSubview(self.activityIndicatorView)
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        activityIndicatorView.startAnimating()
        let url = NSURL(string: SharedClass().clsLink + "/json/POList.cfm")
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
            if(error != nil) {
                // If there is an error in the web request, print it to the console
                println(error.localizedDescription)
            }
            var err: NSError?
            let res = response as! NSHTTPURLResponse!
            if(res != nil){
                if (res.statusCode >= 200 && res.statusCode < 300){
                    self.jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
                    if(err != nil) {
                        // If there is an error parsing JSON, print it to the console
                        println("JSON Error \(err!.localizedDescription)")
                    }
                    var resultsArr: NSArray = self.jsonResult["results"] as! NSArray
//                        println(resultsArr)
                    self.PoList = PoInfo.poInfoWithJSON(resultsArr)
                    self.PoTable!.reloadData()
                    self.activityIndicatorView.stopAnimating()
                    self.activityIndicatorView.hidden = true
                }
                else{
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                    SharedClass().serverAlert(self)
                }
            }else{
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                self.activityIndicatorView.stopAnimating()
                SharedClass().serverAlert(self)
            }
        })
        task.resume()

请帮忙

【问题讨论】:

    标签: json performance swift tableview loading


    【解决方案1】:

    在你的 if 闭包中尝试这个异步作用域dispatch_async(dispatch_get_main_queue())

      dispatch_async(dispatch_get_main_queue()) {
                 if (res.statusCode >= 200 && res.statusCode < 300){
                    self.jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
                    if(err != nil) {
                        // If there is an error parsing JSON, print it to the console
                        println("JSON Error \(err!.localizedDescription)")
                    }
                    var resultsArr: NSArray = self.jsonResult["results"] as! NSArray
                    self.PoList = PoInfo.poInfoWithJSON(resultsArr)
                    self.PoTable!.reloadData()
                    self.activityIndicatorView.stopAnimating()
                    self.activityIndicatorView.hidden = true
                }
      }
    

    【讨论】:

    • 非常感谢!!有用!你能解释一下它为什么这样做吗?
    • 很高兴它成功了。 dispatch_async 总是异步的。并且 dispatch_get_main_queue 使它在主线程上工作。更多你应该check here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多