迅捷清除JSONcaching?

我正在处理一些经常更新的API数据。

我最近发现,在服务器上更新数据时,数据不能在手机上正确更新。

试图解决这个问题几个小时后,我终于试图从我的手机上删除应用程序,然后重新安装。

经过一些进一步的testing,我发现它正在打印出旧的JSON。

一旦我删除应用程序并重新安装它,它成功地打印出正确的更新的JSON。

从那里我收集到,这可能是手机caching旧的JSON数据的问题。

那么我该如何去清除这个caching? 或迫使它提出新的要求。

(我正在使用swiftyJson,虽然我不认为这与这个具体问题有任何关系)

我确实发现了另外一个这样的问题,但已经很老了(Obj-C,2014),没有答案。

以下是我如何获取我的数据:

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 } 

我试着改变我的请求到以下内容:

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

但是这会导致一个错误:“声明之前使用局部variables”请求“

任何帮助搞清楚这将不胜感激!

而不是创build您的请求,

 NSURLRequest(URL: formulaAPI!) 

您应该使用以下方法,以便您可以显式设置caching策略。

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

NSURLRequest(URL:)使用默认的策略NSURLRequestUseProtocolCachePolicy ,超时间隔为60秒。