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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.