2

I'm working with some API data that gets updated frequently.

I recently discovered that the data does not update properly on the phone, when it's updated on the server.

After hours on hours trying to troubleshoot this, I finally simply tried deleting the app from my phone, and reinstalling in. And it worked.

After some further testing I discovered that it's printing out old JSON.

Once I delete the app and reinstall it, it successfully prints out the correct updated JSON.

From that I gathered that it's probably an issue with the phone caching the old JSON data somehow.

So how can I go about clearing this cache in swift? or forcing it to make a new request.

(I'm using swiftyJson, although I don't think that has anything to do with this specific problem)

I did find one other question like this, but it's old (2014 in Obj-C) and there was no answers.

Here's how I get my data:

        var request = NSURLRequest(URL: formulaAPI!)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        var formula = JSON(data: data!)

        // Loop through the api data.
        for (index: String, portfolio: JSON) in formula["portfolio"] {



            // Save the data into temporary variables
            tempStockName = portfolio["name"].stringValue
            tempTicker = portfolio["ticker"].stringValue
            tempPurchasePrice = portfolio["purchase_price"].floatValue.roundTo(2)
            tempWeight = portfolio["percentage_weight"].floatValue
            latestAPIPrice = portfolio["latest_price"].floatValue.roundTo(2)
            tempDaysHeld = portfolio["days_owned"].intValue

            // Continues on for quite a while, but the data in the above segment is definitely getting filled with old JSON data, so the issue is arising before this point
}

I tried changing my request to the following:

var request = init(formulaAPI: NSURL, cachePolicy: NSURLRequestCachePolicy, timeoutInterval: NSTimeInterval)

But that causes an error: "Use of local variable 'request' before it's declaration"

Any help figuring this out would be greatly appreciated!

Mark L
  • 759
  • 2
  • 12
  • 29
  • You should show the code you are using for the download. Are you setting the caching policy explicitly, or using the default? – rdelmar May 29 '15 at 16:31
  • @rdelmar added an example of my code :) – Mark L May 29 '15 at 21:57
  • True creating your request with `requestWithURL:cachePolicy:timeoutInterval:`, and passing `NSURLRequestReloadIgnoringLocalCacheData` to the cachePolicy parameter. – rdelmar May 29 '15 at 22:54
  • @rdelmar thanks for the response :). I don't use requestWithURL anywhere, so where and how would I go about implementing this into my request? – Mark L May 30 '15 at 13:56
  • You do use it, but in the Swift syntax, `var request = NSURLRequest(URL: formulaAPI!)`. Replace that line with one using init(URL theURL: NSURL, cachePolicy cachePolicy: NSURLRequestCachePolicy, timeoutInterval timeoutInterval: NSTimeInterval) – rdelmar May 30 '15 at 16:14
  • @rdelmar Pretty sure I'm doing something completely wrong here. I added my attempt to implement the above code, to the bottom of my question. Again thanks for the help :)! – Mark L May 30 '15 at 16:44
  • I'm not sure what the correct syntax for that method is. Use something like this instead, `var request = NSURLRequest(URL: formulaAPI!, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 30)` – rdelmar May 30 '15 at 16:57
  • That looks like it worked, Thank you so much! Do you want to post an answer in case someone else has the same issue. :)? – Mark L May 30 '15 at 17:01

1 Answers1

11

Instead of creating your request with,

NSURLRequest(URL: formulaAPI!) 

you should use the following method so that you can explicitly set the cache policy.

var request = NSURLRequest(URL: formulaAPI!, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 30)

NSURLRequest(URL:) uses the default came policy, NSURLRequestUseProtocolCachePolicy, and a time out interval of 60 seconds.

rdelmar
  • 103,982
  • 12
  • 207
  • 218