2

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:

  1. is it correct, that isExcludedFromBackup prevents the iOS System from deleting the file?
  2. 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.
  3. If yes (@1.) why is it not working in my case and how can i fix it?
Remo
  • 33
  • 5

1 Answers1

0

1) No, isExcludedFromBackup = true means that when user backups his phone this file will not be included, I wonder why you think that it will not be deleted.

2) The right way to solve your problem is to relocate your database to Documents directory, you can not control caches directory, iOS will clear it when needed.

Arthur Sahakyan
  • 566
  • 3
  • 17
  • @1) I thaught that because like i said i read it (cannot find the site anymore thought). @2) But it would be against the guidelines and also strange to put the database file in the documents directory. The user should not be able to do something with the database file. – Remo Feb 12 '18 at 15:22
  • @Remo ok, first user can not do something with documents Directory, second isExcludedFromBackup name of variable says what about is it. – Arthur Sahakyan Feb 12 '18 at 15:26
  • hm ok i will try the documents directory but i assumed it is the directory which is visible as shared documents in iTunes (and therefore the file would be deletable by the user). If not i will accept your answer. Please hold on :) – Remo Feb 12 '18 at 15:48
  • no the documents directory is no option because its visible in iTunes and therefore deletable by the user. – Remo Feb 12 '18 at 15:53
  • i accept your answer because its true and it directed my into the right direction. I just need to explicitly hide the file from the user. – Remo Feb 12 '18 at 16:10