您好,我正在使用 NSJSONSerialization 在我的应用程序中将 JSON 下载到设备,似乎当我更新 JSON 文件并重新运行下载 JSON 函数时,它不会下载新的 JSON。它只是下载未更新的 JSON 文件。我试过在不同的设备上运行该应用程序,它第一次运行但一旦我再次更新 JSON,它就不会下载新的 JSON。我在想它可能被缓存了,但我不确定。
这是我的代码:
func downloadJSON(){
let p = NSURLRequest(URL: NSURL(string: "https://themartini.co/pythonStuff/quotey.json")!)
let sharedSession = NSURLSession.sharedSession()
let dwn = sharedSession.dataTaskWithRequest(p, completionHandler: { (data : NSData!,re : NSURLResponse!, error: NSError!) -> Void in
if error == nil {
let js : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSArray // downloads JSON and puuts it into an array
println(js)
dispatch_async(dispatch_get_main_queue()){
self.quotes = js as NSArray
NSNotificationCenter.defaultCenter().postNotificationName("dataTaskFinished", object: nil)
}
}
})
dwn.resume()
}
有什么想法吗?谢谢。
最佳答案
您应该将正确的缓存策略传递给您的 NSURLRequest
。有一个 alternative constructor包括这个参数。
let p = NSURLRequest(URL: url,
cachePolicy: ReloadIgnoringLocalCacheData,
timeoutInterval: 15.0)
关于ios - NSJSONSerialization 不更新(可能被缓存),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27284055/