1
votes

I'm using Cache framework for caching in my application. Cache framework can provide hybrid storage with expire option. I've set .never for both storages (disk and memory), but cache still can expire.

In my app i need to cache some Data() for forever, and update it if necessary (but not frequently). Speed of getting this cache is not that necessary.

Is it possible using this framework? May i need to use just DiskStorage, instead of Storage? I think it can expire, because it saving into system memory (which is RAM as i think) all the time, so iOS can clean it if needed, but can iOS clean disk storage?

1

1 Answers

1
votes

So, i've found something about this in Apple Guidelines:

Use the "do not back up" attribute for specifying files that should remain on device, even in low storage situations. Use this attribute with data that can be recreated but needs to persist even in low storage situations for proper functioning of your app or because customers expect it to be available during offline use. This attribute works on marked files regardless of what directory they are in, including the Documents directory. These files will not be purged and will not be included in the user's iCloud or iTunes backup. Because these files do use on-device storage space, your app is responsible for monitoring and purging these files periodically.

I wrote simple function for this:

func setExludedFromBackup() {
    var url = try? FileManager.default.url(
        for: .documentDirectory,
        in: .userDomainMask,
        appropriateFor: nil,
        create: true).appendingPathComponent("MyFolder")
    var resuorceValues = URLResourceValues()
    resuorceValues.isExcludedFromBackup = true
    try? url?.setResourceValues(resuorceValues)
 // print(try? url?.resourceValues(forKeys: [.isExcludedFromBackupKey]).isExcludedFromBackup)
 // you can check if your directory excluded from backup (it excluded if "Optional(Optional(true))" printed)
}

Yes, it's Documents folder, because other folders still continue to be cleared.

You simply call this func somewhere (i've used AppDelegate), and check if directory is already excluded from backup before calling it. That protect "MyFolder" in Documents directory from deleting, if iOS has ran out of memory! So my data will be available as long as i want to. But you must include after-time deleting mechanism too, because this data will never be deleted automatically and on-device memory space will not be released. In my case i just used Cache framework's expiry property

P.S. Im not sure how will Apple react to this, while inspecting your app for AppStore, so tell them about offline use in your app and what you did with documents folder, don't forget to describe your deleting mechanism. I hope that doesn't break Apple Guidelines and app will not be rejected.