【问题标题】:JSON not updating in swiftJSON没有迅速更新
【发布时间】:2015-03-21 09:26:34
【问题描述】:

我正在开发一个使用来自 url 的 JSON 的 swift 应用程序。但是在 JSON 中进行更改时,它不会在应用程序中更新。这是我正在使用的代码

let urlAsString = "http://domain/json.json"
    let url = NSURL(string: urlAsString)!
    let urlSession = NSURLSession.sharedSession()

    let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
        if (error != nil) {
            println(error.localizedDescription)
        }
        var err: NSError?

        var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
        if (err != nil) {
            println("JSON Error \(err!.localizedDescription)")
        }
        var i = 1
        while i <= jsonResult.count / 2 {
            let title = i.description + "t"
            self.feeds.insert(jsonResult[i.description] as NSString, atIndex: 0)
            self.feedst.insert(jsonResult[title] as NSString, atIndex: 0)
            i++
            }
        dispatch_async(dispatch_get_main_queue(), {
            self.tableView.reloadData()
        })
    })
    jsonQuery.resume()

我尝试进行 som 调试并提出问题可能出在这部分:

var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary

我认为 Apple 正在缓存旧值

【问题讨论】:

    标签: ios json swift


    【解决方案1】:

    共享会话可能会缓存数据并阻止刷新。您需要创建自己的 NSURLSession 并设置请求缓存策略。请参阅此答案以获取代码 example

    【讨论】:

      猜你喜欢
      • 2020-01-10
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多