简体   繁体   English

在iOS Swift的TableView上显示数据的延迟

[英]Delay in displaying data on tableView in ios swift

Hi, 你好

I'm trying to parse data on to the listview and am able to get it and display it on the tableView but the problem is it is taking hell lot of time to display it on the tableview. 我正在尝试将数据解析到listview上,并能够将其显示在tableView上,但问题是要花费大量时间在tableview上显示它。 Please find the my code below. 请在下面找到我的代码。

func jsonParsing()
    {
        activityIndicatorView.startAnimating()

        let session = NSURLSession.sharedSession()
        let request = NSMutableURLRequest (URL: deviceListURL)
        request.HTTPMethod = "GET"

        let task = session.dataTaskWithRequest(request, 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?
            if(data != nil) {
                var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSMutableArray

                //println("Data: \(jsonResult)")

                var dataDict: NSDictionary
                for dataDict : AnyObject in jsonResult {
                    var device_id: NSString = dataDict.objectForKey("deviceId") as! NSString
                    var device_name: NSString = dataDict.objectForKey("deviceName") as! NSString
                    var device_status: NSInteger = dataDict.objectForKey("status") as! NSInteger

                    let dictionary = [self.deviceID: device_id, self.deviceName: device_name, self.Status: device_status]
                    self.myObject.addObject(dictionary)
                }

                println("My object = %@", self.myObject)
                println(self.myObject.count)

                if self.myObject.count != 0 {
                    self.reloadTable()

                }
            }

            if err != nil {
                // If there is an error parsing JSON, print it to the console
                println("JSON Error \(err!.localizedDescription)")
            }


        })

        task.resume()

    }

The completion handler is running in a background queue, not the main thread. 完成处理程序在后台队列(而不是主线程)中运行。 UI updates have to happen on the main thread, though. 但是,UI更新必须在主线程上进行。

Try calling reloadTable() on the main thread: 尝试在主线程上调用reloadTable()

dispatch_sync(dispatch_get_main_queue(), {
    self.reloadTable()
})

(I just typed this in here untested, so I hope it works this way) (我只是在这里输入了未经测试的内容,所以我希望它可以这样工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM