简体   繁体   中英

What directory should I store analytics data in on iOS?

I'm trying to store analytics data that is saved locally about a user's actions so it can be uploaded later when the user has an internet connection. I'd like the data to be stored locally and not deleted between subsequent opens of the app under normal circumstances. I do not want the data to be synced to iCloud. I'd also ideally like the data to be preserved between updates. It's fine if the data gets deleted in cases of low space.

I'm getting different answers from different sources about where to store the data- either in NSCachesDirectory or NSLibraryDirectory. Note NSCachesDirectory is a subdirectory of NSLibraryDirectory, eg. the filesystem looks like Application_Home/Library/Caches/.

According to the official documentation: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW9 implies I should use NSCachesDirectory to store the data and it is not deleted under most circumstances. It also implies NSLibraryDirectory is synced to iCloud.

According to these answers: How can I get a writable path on the iPhone? , https://stackoverflow.com/a/5444762/340520/ , When are files from NSCachesDirectory removed? , NSCachesDirectory is not preserved between app updates and claims that I must constantly recreate the NSCachesDirectory. The first answer also implies NSLibraryDirectory is the best place to store the data. However those answers are two years old.

According to the documentation and this answer: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW10 , https://stackoverflow.com/a/8830746/340520 , NSCachesDirectory ARE preserved between app updates.

Localytics' iOS SDK stores their data in NSCachesDirectory: https://github.com/twobitlabs/Localytics-iOS/blob/master/src/LocalyticsDatabase.m Mixpanel's iOS SDK stores their data in NSLibraryDirectory: https://github.com/mixpanel/mixpanel-iphone/blob/master/Mixpanel/Mixpanel.m

Between all these sources, I've been leaning toward using NSCachesDirectory, but I'm not confident that the data won't get regularly deleted under some circumstances.

NSCachesDirectory is the wrong place to store persistent information that you will need across app starts or even device re-starts.

To prove my point try this ...

  1. Get a iPhone device that has only 8GB disk space.
  2. Use your app to write a file in the NSCachesDirectory.
  3. Start downloading random apps to fill up the disk space. Very soon you will see the Storage limit dialog shown by the OS.
  4. Now just re-start your phone, start the app and see if you can find your file that you wrote.

If the first time you find the file, try the experiment again and you will find your file missing. During the device startup, if you see the device logs you will notice logs indicating purging directory to make space etc.

Use NSCachesDirectory to store information you can keep downloading from your server. Not information that you need to upload to your server.

The Caches directory should only be used for files that your app can easily replace if they are deleted. The Caches directory is may or may not be purged during an app update and possibly if the device runs out of storage space. Only use this for temporary files or files you can easily replace.

My first choice would be the Library/Application Support directory ( NSApplicationSupportDirectory ). Please note that this directory is not created by default. Your app must create it on first startup. This path is kept during app updates (like most of the app sandbox) and it is backed up via iTunes (or iCloud) device backup.

I think the best place is Library/Application Support (NSApplicationSupportDirectory) refer How do I prevent files from being backed up to iCloud and iTunes? for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM