ios - 在 ios swift 中延迟在 tableView 上显示数据

标签 ios json swift reloaddata

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

    }

最佳答案

完成处理程序在后台队列中运行,而不是主线程。不过,UI 更新必须在主线程上进行。

尝试在主线程上调用reloadTable():

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

(我只是在这里输入这个未经测试,所以我希望它能这样工作)

关于ios - 在 ios swift 中延迟在 tableView 上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666362/

相关文章:

javascript - 可以上传文件并解析json结果吗?

generics - Swift - 无法将 BST 节点转换为通用节点

ios - 来自 NSURLSession 的动态数据的单元格行的高度

ios - 在 RestKit 中展平请求映射层次结构

objective-c - UIPicker 从 plist 访问项目

json - 如何在 Swift 3 中使用循环打印 JSON 值?

javascript - 如何在我的 HTML 网页中使用 JSON 文件

ios - 带核心数据的 watchOS 2

ios - 为什么没有调用 didDeselectRowAtIndexPath

ios - 具有多重采样的 Metal 屏幕外绘图