optimizely / swift-sdk

Swift SDK for Optimizely Feature Experimentation and Optimizely Full Stack (legacy)
https://www.optimizely.com/products/full-stack/
Apache License 2.0
21 stars 30 forks source link

Datafile Cache Location #485

Closed rhys-rant closed 1 year ago

rhys-rant commented 1 year ago

Hi there!

Is there any way to configure the location of the cached datafile?

Currently it seems to store to the application's Documents folder, which seems inappropriate when there's a Caches directory on iOS. If an app opens itself to the Files app, it will also expose the datafile via the Documents folder, meaning end users will see that datafile.

Ideally, the SDK should default to storing a cached datafile in the Caches directory, or allow the user to configure where to store it.

If this functionality exists in the SDK, I'd really appreciate some pointers on what I need to configure! Thanks :)

jaeopt commented 1 year ago

@rhys-rantmedia Thanks for reporting. Currently there is no easy way to override the location. We'll look into it. https://jira.sso.episerver.net/browse/FSSDK-9013

russell-loube-optimizely commented 1 year ago

Hi @rhys-rantmedia, thanks for your patience on this. We have just pulled a spike research story into our current sprint to identify potential solutions to allow configuration of the datafile cache location. We'll keep you posted here.

russell-loube-optimizely commented 1 year ago

Hi again @rhys-rantmedia , some findings below.

The Caches directory is only for temporary storage and iOS clears this cache anytime the app runs out of memory. This is the main reason why we are saving the datafile in the Documents directory, which is permanent storage and not subject to automatic deletion by iOS in periods of high memory consumption.

It is possible to supply your own OPTDatafileHandler interface with the following methods:

func saveDatafile(sdkKey: String, dataFile: Data)

func loadSavedDatafile(sdkKey: String) -> Data?

func isDatafileSaved(sdkKey: String) -> Bool

func removeSavedDatafile(sdkKey: String)

If desired, you can use our default datafile handler class and override the above methods to store and retrieve the datafile in a customized manner.

For tvOS and macOS, we do use the cache directory as seen in the below code snippet:

#if os(tvOS) || os(macOS)

let directory = FileManager.SearchPathDirectory.cachesDirectory

#else

let directory = FileManager.SearchPathDirectory.documentDirectory

#endif

I hope the above helps somewhat. Please let us know if you're able to move forward with such an approach, or if there's any other information we can provide to assist. Thanks!