1

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.

Community
  • 1
  • 1
sskates
  • 1,478
  • 1
  • 13
  • 12
  • Testing on an iPhone 4 with iOS 6.1.2 indicates that NSCachesDirectory is NOT deleted between terminations of the app. – sskates Mar 01 '13 at 03:57

3 Answers3

1

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.

Kris Subramanian
  • 1,850
  • 18
  • 15
0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I would like for it NOT to be synced between different instances of the app through iTunes/iCloud. The data represents a temporary store of analytics events that hasn't been uploaded to my servers yet. – sskates Mar 01 '13 at 04:16
  • In that case, I'd suggest creating your own folder under "Library" and use the information in [Technical Q&A 1719](https://developer.apple.com/library/ios/#qa/qa1719/_index.html) to prevent the files from being backed up. – rmaddy Mar 01 '13 at 04:21
0

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.

Prashant Rane
  • 436
  • 4
  • 11