My Problem: I have my SQLite database file in the cache directory and set isExcludedFromBackup to true because i read, that doing so will prevent the iOS system from deleting it in case of full storage. But it gets deleted nonetheless. This was observed in development on real device iPhone 5s with iOS 10.3.3.
The SQLite database file is created by the SQLite.swift API which could be important but not necessary the reason. https://github.com/StephenCelis/SQLite.swift
Creating the file with 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
Setting the isExcludedFromBackup flag:
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)
}
}
This is called with the same filePath, which is given to the Connection in first Code block. I also have checked if the flag is being set correctly by getting the ResourceValues from that file before and after setting it to true and it gets changes correctly.
Because its necessary for offline usage it should be ok with the store guidelines.
Questions:
- is it correct, that isExcludedFromBackup prevents the iOS System from deleting the file?
- If not, how can i prevent the system from deleting it? Because of the guidelines i think i cannot store the file in the .documentsDirectory and i would not know which directy i should use. I would prefer a solution where i do not need to relocate the database file.
- If yes (@1.) why is it not working in my case and how can i fix it?