gpt4 book ai didi

ios - 快速清除 JSON 缓存?

转载 作者:搜寻专家 更新时间:2023-11-01 05:49:07 26 4
gpt4 key购买 nike

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

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

在尝试解决此问题数小时后,我终于尝试从手机中删除该应用程序,然后重新安装。它成功了。

经过进一步测试后,我发现它正在打印出旧的 JSON。

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

据我所知,这可能是手机以某种方式缓存旧 JSON 数据的问题。

那么我怎样才能快速清除这个缓存呢?或者强制它提出新的请求。

(我正在使用 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)

但这会导致错误:“在声明之前使用局部变量‘request’”

如果您能帮助解决这个问题,我们将不胜感激!

最佳答案

而不是创建您的请求,

NSURLRequest(URL: formulaAPI!) 

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

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

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

关于ios - 快速清除 JSON 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30534272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com