简体   繁体   English

我应该在iOS上的哪个目录中存储分析数据?

[英]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. 我正在尝试存储在本地保存的有关用户操作的分析数据,以便以后在用户具有Internet连接时可以上载。 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. 我不希望数据同步到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. 我从不同的来源那里得到了关于将数据存储在NSCachesDirectory或NSLibraryDirectory中的不同答案。 Note NSCachesDirectory is a subdirectory of NSLibraryDirectory, eg. 注意NSCachesDirectory是NSLibraryDirectory的子目录,例如。 the filesystem looks like Application_Home/Library/Caches/. 文件系统看起来像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. 根据官方文档: http : //developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW9暗示我应该使用NSCachesDirectory来存储数据,并且在大多数情况下不会删除它。 It also implies NSLibraryDirectory is synced to iCloud. 这也意味着NSLibraryDirectory已同步到iCloud。

According to these answers: How can I get a writable path on the iPhone? 根据这些答案: 如何在iPhone上获得可写路径? , https://stackoverflow.com/a/5444762/340520/ , When are files from NSCachesDirectory removed? https: //stackoverflow.com/a/5444762/340520/何时从NSCachesDirectory中删除文件? , NSCachesDirectory is not preserved between app updates and claims that I must constantly recreate the NSCachesDirectory. ,因此在应用程序更新和要求我必须不断重新创建NSCachesDirectory之间不会保留NSCachesDirectory。 The first answer also implies NSLibraryDirectory is the best place to store the data. 第一个答案还暗示NSLibraryDirectory是存储数据的最佳位置。 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. 根据文档和此答案: http : //developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW10https ://stackoverflow.com/a/8830746/340520,NSCachesDirectory在应用程序更新之间保留。

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 Localytics的iOS SDK将其数据存储在NSCachesDirectory中: https : //github.com/twobitlabs/Localytics-iOS/blob/master/src/LocalyticsDatabase.m Mixpanel的iOS SDK将其数据存储在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,但我不确定在某些情况下不会定期删除数据。

NSCachesDirectory is the wrong place to store persistent information that you will need across app starts or even device re-starts. NSCachesDirectory是存储永久性信息的错误位置,您在整个应用程序启动甚至设备重新启动时都需要这些信息。

To prove my point try this ... 为了证明我的观点,尝试一下...

  1. Get a iPhone device that has only 8GB disk space. 获取仅具有8GB磁盘空间的iPhone设备。
  2. Use your app to write a file in the NSCachesDirectory. 使用您的应用程序在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. 使用NSCachesDirectory存储可以继续从服务器下载的信息。 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. 在应用程序更新期间以及如果设备存储空间不足的情况下,可能会清除或可能不会清除Caches目录。 Only use this for temporary files or files you can easily replace. 仅将其用于临时文件或可以轻松替换的文件。

My first choice would be the Library/Application Support directory ( NSApplicationSupportDirectory ). 我的第一个选择是Library/Application Support目录( 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. 此路径在应用程序更新过程中(与大多数应用程序沙箱一样)保留,并通过iTunes(或iCloud)设备备份进行备份。

I think the best place is Library/Application Support (NSApplicationSupportDirectory) refer How do I prevent files from being backed up to iCloud and iTunes? 我认为最好的地方是Library/Application Support (NSApplicationSupportDirectory)请参阅如何防止文件备份到iCloud和iTunes? for details. 有关详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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