1

I have a page that loops JSON data and show in a tableview. I get the correct correctly but when it shows on the page, it is very slow. I tried to println the json to see how fast it retrieves the json data and it was very fast. One more thing which is very odd is when it is loading, if I drag the page, everything will show instantly. Below is the code I put in viewdidload function

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

please help

Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
Jun Luo
  • 139
  • 1
  • 13

1 Answers1

1

try this async scope dispatch_async(dispatch_get_main_queue()) in your if closure like

  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
            }
  }
Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • Thank you so much!! It works! Can you explain why it is doing it? – Jun Luo Jun 03 '15 at 16:48
  • glad that it worked. dispatch_async is always asynchronous. and dispatch_get_main_queue makes it work on the main thread. for more you should [check here](http://stackoverflow.com/questions/17490286/does-dispatch-asyncdispatch-get-main-queue-wait-until-done) – Özgür Ersil Jun 03 '15 at 16:54