iOS:防止 .cacheDirectory 中的文件在存储空间已满警告时被删除
iOS: Prevent file in the .cacheDirectory from being deleted on full storage warning
我的问题:
我在缓存目录中有我的 SQLite 数据库文件,并将 isExcludedFromBackup 设置为 true 因为我读到,这样做将防止 iOS 系统在存储满的情况下删除它。但它仍然被删除。这是在真实设备 iPhone 5s 和 iOS 10.3.3.
的开发中观察到的
SQLite 数据库文件是由 SQLite.swift API 创建的,这可能是重要但不是必需的原因。 https://github.com/StephenCelis/SQLite.swift
创建文件 SQLite.swift:
let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
let databaseName = "name.extension"
let filePath = "\(path)/\(databaseName)"
self.db = try! Connection(filePath) // creates the file, if it does not exists
设置 isExcludedFromBackup 标志:
func excludeSQLiteFileFromBackup(path: String) {
let exists = FileManager.default.fileExists(atPath: path)
if !exists {
MyLogger.log("excludeSQLiteFileFromBackup: file not created yet.", aClass: self.classForCoder)
return
}
var url = URL(fileURLWithPath: path)
do {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
} catch let error as NSError {
MyLogger.log("excludeSQLiteFileFromBackup: \(error)", aClass: self.classForCoder)
}
}
这是使用相同的文件路径调用的,该文件路径在第一个代码块中提供给连接。我还通过在将该文件设置为 true 之前和之后从该文件中获取 ResourceValues 来检查该标志是否设置正确,并且它正确地进行了更改。
因为它是离线使用所必需的,所以符合商店指南应该没问题。
问题:
- 是否正确,即 isExcludedFromBackup 阻止 iOS 系统删除文件?
- 如果没有,如何防止系统删除它?由于指南,我认为我不能将文件存储在 .documentsDirectory 中,而且我不知道应该使用哪个目录。我更喜欢不需要重新定位数据库文件的解决方案。
- 如果是 (@1.) 为什么它在我的情况下不起作用,我该如何解决?
1) 不是,isExcludedFromBackup = true
是指用户备份他的phone这个文件时不会包含,不知道你为什么认为它不会被删除。
2) 解决你的问题的正确方法是将你的数据库迁移到Documents目录,你不能控制缓存目录,iOS会在需要的时候清除它。
我的问题: 我在缓存目录中有我的 SQLite 数据库文件,并将 isExcludedFromBackup 设置为 true 因为我读到,这样做将防止 iOS 系统在存储满的情况下删除它。但它仍然被删除。这是在真实设备 iPhone 5s 和 iOS 10.3.3.
的开发中观察到的SQLite 数据库文件是由 SQLite.swift API 创建的,这可能是重要但不是必需的原因。 https://github.com/StephenCelis/SQLite.swift
创建文件 SQLite.swift:
let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
let databaseName = "name.extension"
let filePath = "\(path)/\(databaseName)"
self.db = try! Connection(filePath) // creates the file, if it does not exists
设置 isExcludedFromBackup 标志:
func excludeSQLiteFileFromBackup(path: String) {
let exists = FileManager.default.fileExists(atPath: path)
if !exists {
MyLogger.log("excludeSQLiteFileFromBackup: file not created yet.", aClass: self.classForCoder)
return
}
var url = URL(fileURLWithPath: path)
do {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
} catch let error as NSError {
MyLogger.log("excludeSQLiteFileFromBackup: \(error)", aClass: self.classForCoder)
}
}
这是使用相同的文件路径调用的,该文件路径在第一个代码块中提供给连接。我还通过在将该文件设置为 true 之前和之后从该文件中获取 ResourceValues 来检查该标志是否设置正确,并且它正确地进行了更改。
因为它是离线使用所必需的,所以符合商店指南应该没问题。
问题:
- 是否正确,即 isExcludedFromBackup 阻止 iOS 系统删除文件?
- 如果没有,如何防止系统删除它?由于指南,我认为我不能将文件存储在 .documentsDirectory 中,而且我不知道应该使用哪个目录。我更喜欢不需要重新定位数据库文件的解决方案。
- 如果是 (@1.) 为什么它在我的情况下不起作用,我该如何解决?
1) 不是,isExcludedFromBackup = true
是指用户备份他的phone这个文件时不会包含,不知道你为什么认为它不会被删除。
2) 解决你的问题的正确方法是将你的数据库迁移到Documents目录,你不能控制缓存目录,iOS会在需要的时候清除它。