0

I'm working on an swift app which is using JSON from an url. But when making changes in the JSON it doesn't update in the application. This is the code I'm using

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

I tried to make som de-bugging and come up with that the problem might be in this part:

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

I think Apple is caching the old values

ntoonio
  • 3,024
  • 4
  • 24
  • 28

1 Answers1

2

The shared session might cache the data and prevents a refresh. You need to create your own NSURLSession and set the request cache policy. See this answer for a code example.

Community
  • 1
  • 1
Thomas
  • 2,338
  • 2
  • 23
  • 32