在 iOS 中清除缓存目录的最佳做法是什么? [英] What are the best practice for clearing cache directory in iOS?

查看:81
本文介绍了在 iOS 中清除缓存目录的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSURLRequestReturnCacheDataElseLoad 缓存策略将网页加载到 WKWebview.我不需要清除缓存,除非服务器明确告诉我这样做.但是一旦服务器告诉我这样做,我就会面临清除缓存的麻烦.

I load a webpage to WKWebview using NSURLRequestReturnCacheDataElseLoad cache policy.There is no need for me to clear the cache unless the server explicitly tells me to do it.But I am facing trouble clearing the cache , once the server tells me to do it.

大多数答案和文章都表明 removeAllCachedResponses 有效,尽管周围流传着一些抱怨关于 NSURLCache 无法与 NSURLSession 或 UIWebView 一起正常工作的问题.我无法让它在 iOS 8.4 或 9.3 模拟器中为我工作.

Most of the answers and articles suggests that removeAllCachedResponses works , though there are several complaints circulating around about NSURLCache not working properly with NSURLSession or UIWebView .I couldn't get it to work for me either in iOS 8.4 or 9.3 simulators.

所以我使用以下代码以编程方式清除缓存目录中的所有文件.我在 WKWebview 中使用的网站缓存文件驻留在Application/Cache/bundleidentifier.尽管如此,我尝试删除所有我可以删除的文件.当我运行代码时,我在尝试删除/Snapshots 时遇到错误.现在这让我想知道缓存目录中还有哪些其他文件我不应该篡改?我知道 SDWebImage 缓存和其他几个文件驻留在此目录中.但是,无论如何我都需要清除 SDWebImage 缓存.

So I used the following code to clear all the files in cache directory Programatically. The cached files of website that I use in my WKWebview resides in Application/Cache/bundleidentifier. Though , I try and delete all the files I can.When I run the code , I get an error trying to delete /Snapshots .Now this made me wonder what are some other files in cache directory that I should not tamper with ? I know the SDWebImage cache and few other files resides in this directory.But, I need to clear the SDWebImage cache anyways.

这是我用来清除缓存目录的代码:

Here is the code I used to clear cache directory :

public func clearCache(){
    let cacheURL =  NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first!
    let fileManager = NSFileManager.defaultManager()
    do {
        // Get the directory contents urls (including subfolders urls)
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( cacheURL, includingPropertiesForKeys: nil, options: [])
        for file in directoryContents {
            do {
                  try fileManager.removeItemAtURL(file)
                }
                catch let error as NSError {
                    debugPrint("Ooops! Something went wrong: \(error)")
                }

            }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

现在,这是一个好的做法吗?我是否缺少任何明显的方法来实现相同的目标?

Now , is this a good practice ? Are there any obvious methods that I am missing to achieve the same?

推荐答案

清除缓存目录完全没问题.是的,遍历内容就是这样做的方式.

It's totally fine to clear the caches directory. And yes, iterating through the contents is the way it is done.

以下是 Apple 的声明:

Here's what Apple say:

使用此目录编写任何特定于应用程序的支持文件应用程序可以轻松重新创建.您的应用程序通常负责管理此目录的内容以及添加和删除需要的文件.

Use this directory to write any app-specific support files that your app can re-create easily. Your app is generally responsible for managing the contents of this directory and for adding and deleting files as needed.

文件系统概述

这篇关于在 iOS 中清除缓存目录的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆