swift NSURLSession POST请求后重新加载数据的时间是什么

时间:2016-03-22 15:56:29

标签: ios json uitableview swift2

我试图在获取JSON后在tableView中显示一些数据。但是,使用tableView.reloadData()失败了。菜单延迟。

我的情况是tableView将在获取JSON 14-20秒后重新加载数据,但是当用户激活表时它会立即重新加载数据。

斯威夫特:

  func getJson(word: String){
        let url: NSURL = NSURL(string: "MyPHP.php")!
        let session = NSURLSession.sharedSession()

        let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
        request.HTTPMethod = "POST"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

        let bodydata = "label=\(word)"
        request.HTTPBody = bodydata.dataUsingEncoding(NSUTF8StringEncoding)

        if bodydata != " " {

            let task = session.dataTaskWithRequest(request) {
                (data, response, error)  -> Void in

                let httpResponse = response as! NSHTTPURLResponse
                let statusCode = httpResponse.statusCode

                if(statusCode == 200) {

                    self.libraryArray.removeAll()
                    do{
                        let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
                        print(json)
                        if let book = json["Book"] as? [[String: AnyObject]]{
                            for books in book {
                                if let BookName =  book["Name"] as? [String: AnyObject] {
                                    let name =  BookName["name"] as? String
                                    let author =  BookName["author"] as? String

                                    let bookDict = [
                                        "name": name,
                                        "id": id,
                                        "author": author]

                                        self.libraryArray.append(bookDict)
                                        self.tableView.reloadData() // has 14- 20 seconds delay, but the JSON has already received 

                                }
                            }
                        }
                    }catch {
                        print("error:\(error)")
                    }
                }
            }
            task.resume()
          // self.tableView.reloadData()  if i try to reload here, it will not be executed.
        }
    }


@IBAction func Next(sender: AnyObject) {
            self.getJson("someString")
         //self.tableView.reloadData()  if i try to reload here, it will be fail.

        }

延迟的问题是什么?怎么解决? 重新加载tableView的正确时机是什么?

2 个答案:

答案 0 :(得分:1)

将此self.tableView.reloadData()替换为此

 dispatch_async(dispatch_get_main_queue(), { () -> Void in

            self.tableView.reloadData()

})

答案 1 :(得分:0)

当你没有完成解析json时,你要求表重新加载。你在每本书解析后都这样做。完成解析后你应该这样做。延迟的唯一解释是你有一个巨大的数据,需要花费所有的时间进行解析!这可能吗?

<?xml version="1.0" encoding="UTF-8"?>
  <mule xmlns=http://www.mulesoft.org/schema/mule/c... ....>
     <spring:beans>
        <spring:import resource="si-config.xml" />
     </spring:beans>