我正在处理一些经常更新的 API 数据。
我最近发现,当数据在服务器上更新时,数据在手机上无法正确更新。
经过几个小时的尝试解决此问题后,我终于尝试从手机中删除该应用程序,然后重新安装。它成功了。
经过进一步测试,我发现它打印出旧的 JSON。
一旦我删除应用程序并重新安装它,它就会成功打印出正确的更新后的 JSON。
据我推测,这可能是手机以某种方式缓存旧 JSON 数据的问题。
那么我怎样才能快速清除这个缓存呢?或者强迫它提出新的请求。
(我正在使用 swiftyJson,尽管我认为这与这个特定问题没有任何关系)
我确实找到了另一个这样的问题,但它很旧(2014 年在 Obj-C 中)并且没有答案。
以下是我获取数据的方式:
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)
但这会导致错误:“在声明之前使用局部变量‘请求’”
任何帮助解决这个问题的帮助将不胜感激!