Json 从NSURLConnection加载UITableView时,数据加载时间过长

Json 从NSURLConnection加载UITableView时,数据加载时间过长,json,xcode,multithreading,swift,nsurlconnection,Json,Xcode,Multithreading,Swift,Nsurlconnection,我的应用程序显示存储在服务器上的信息,这些信息会不断更新。因此,在浏览数据时,我将使用以下类检索的数据加载到每个页面 public class GetRemoteData { class func getDataFromServer(success: ((svrData: NSData!) -> Void)) { //Set http connection settings let httpMethod = "GET" let timeout = 10

我的应用程序显示存储在服务器上的信息,这些信息会不断更新。因此,在浏览数据时,我将使用以下类检索的数据加载到每个页面

public class GetRemoteData {

  class func getDataFromServer(success: ((svrData: NSData!) -> Void)) {
    //Set http connection settings
    let httpMethod = "GET"
    let timeout = 10
    let url = NSURL(string: ViewController.gVariables.gUrl)
    let urlRequest = NSMutableURLRequest(URL: url!,
        cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 10.0)
    let queue = NSOperationQueue()
    //Initiate connection to server
    NSURLConnection.sendAsynchronousRequest(
        urlRequest,
        queue: queue,
        completionHandler: {(response: NSURLResponse!,
            data: NSData!,
            error: NSError!) in
            //If data recieved load into svrData
            if data.length > 0 && error == nil{
                let json = NSString(data: data, encoding: NSASCIIStringEncoding)
                if let urlData = json {
                    let data = json!.dataUsingEncoding(NSUTF8StringEncoding)
                    success(svrData: data)
            }
            }else if data.length == 0 && error == nil{
                ViewController.gVariables.gError = "No data was recieved, please check your connection"
            } else if error != nil{
                ViewController.gVariables.gError = "Error occurred - \(error), please check your connection"
            }
        }
    )
  }
}
在所有使用该类的ViewController之间移动时,数据加载速度很快,但在向后移动时,尤其是第一次通过sendAsynchronousRequest而未成功通过时。ViewController将继续并显示一个空白表。然后,上面的代码这次再次成功地在线程上运行,并显示数据,但这可能需要30秒!以下代码由ViewDidLoad中的ViewController使用

GetRemoteData.getDataFromServer { (svrData) -> Void in
    let jsonDict =    NSJSONSerialization.JSONObjectWithData(svrData, options: nil, error: nil) as! NSDictionary
    if (ViewController.gVariables.gError != "") {
         let alertController = UIAlertController(title: "Warning", message:
         ViewController.gVariables.gError, preferredStyle: UIAlertControllerStyle.Alert)
         alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))  
         alertController.presentViewController(alertController, animated: true, completion: nil)
    }
    if let items = jsonDict["products"] as? NSArray {
         for jsonItem in items {
             if let name = jsonItem.valueForKey("name") as? String
             {
                 self.brandNames.addObject(name)
             }
             if let code = jsonItem.valueForKey("code") as? String
             {
                 self.brandCodes.addObject(code)
             }                        
         }
     }
     self.tableView.reloadData()
}

有人能告诉我如何克服这个时间延迟问题吗?

以上两个响应都通过在主线程上运行Json代码解决了我的问题
dispatch\u async(dispatch\u get\u main\u queue())
reload tableview这种方式:
dispatch\u async(dispatch\u get\u main\u queue()){self.tableview.reloadData()}
对我有效,谢谢我解释过你应该把你的查询json放到
dispatch\u async(dispatch\u get\u main\u queue())
以这种方式重新加载tableview:
dispatch\u async(dispatch\u get\u main\u queue()){self.tableview.reloadData()}
对我有用,谢谢我解释过了,你应该将json查询放在
dispatch\u async(dispatch\u get\u main\u queue())
以这种方式重新加载tableview:
dispatch\u async(dispatch\u get\u main\u queue()){self.tableView.reloadData()}
这对我很有用,谢谢