ios - NSJSONSerialization 不更新(可能被缓存)

标签 ios iphone json swift

您好,我正在使用 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/

相关文章:

php - 如何在 iPhone 上安装 (L|M)AMP 堆栈?

iphone - 如何使用 UIImagepickercontroller 自定义相机屏幕

javascript - 个性化列表列 - Nodejs(NPM)

android - 如何在一个 View 中收集具有相同值的json,在另一个 View 中收集其他值并制作动态 View

mysql - 出于机器学习目的存储多个来源的数据的最佳实践

ios - 使用 localhost 使用 iOS 9.2 和 Xcode 7.2 访问本地服务器

ios - 将 UISearchBar 结果呈现为 UIPopoverPresentationController iOS 9

iphone - iPad:我可以使用编译器指令来区分模拟器和设备吗?

ios - iOS 上的标准位置服务耗电

ios - 如何使用 UITableView 和分段控件进行分页