iOS数据存储

一  An iOS app operating within its own sandbox directory

在自己的沙盒目录下运行的iOS应用程序

An app is generally prohibited from accessing or creating files outside its container directories. One exception to this rule is when an app uses public system interfaces to access things such as the user’s contacts or music. In those cases, the system frameworks use helper apps to handle any file-related operations needed to read from or modify the appropriate data stores.

应用程序通常不允许在它的容器目录之外访问或创建文件。一个例外是当应用程序使用公共系统接口去访问例如用户的联系人或音乐。在这些场景,系统框架使用开发者帮助文档去处理需要从适当的数据存储读取或修改的任何文件相关的操作。

二  Where You Should Put Your App’s Files

To prevent the syncing and backup processes on iOS devices from taking a long time, be selective about where you place files. Apps that store large files can slow down the process of backing up to iTunes or iCloud. These apps can also consume a large amount of a user's available storage, which may encourage the user to delete the app or disable backup of that app's data to iCloud. With this in mind, you should store app data according to the following guidelines:

为了防止在iOS设备同步和备份使用很长的时间,你的文件存储位置是可以被选择的。存储大文件的应用程序会减慢备份到iTunes或iCloud的过程,这些应用也会消耗大量用户可用空间。这些都会促使用户删除应用或者不再备份应用的数据到iCloud。基于此,你应该按照如下指南进行存储应用数据。

Put user data in Documents/. User data generally includes any files you might want to expose to the user—anything you might want the user to create, import, delete or edit. For a drawing app, user data includes any graphic files the user might create. For a text editor, it includes the text files. Video and audio apps may even include files that the user has downloaded to watch or listen to later.

将用户数据存放在Documents/. 路径。用户数据一般包括你可能想暴露给用户的任何文件,你可能想让用户创建、引入、删除或编辑的任何数据。对于一个绘制应用程序,用户数据包含用户可能创建的任何图表文件。对于一个文本编辑器,它包含一些文本文件。视频和音频类应用可能甚至包含用户已经下载后要观看或收听的文件。

Put app-created support files in the Library/Application support/ directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.

将应用创建的支持文件放在路径Library/Application support/ directory,一般而言,这个目录包含应用运行需要的但是对用户而言应该要隐藏的文件。该目录还可以包含从app bundle中加载的数据文件、配置文件、模板和修改版本的资源

Remember that files in Documents/ and Application Support/ are backed up by default. You can exclude files from the backup by calling -[NSURL setResourceValue:forKey:error:] using the NSURLIsExcludedFromBackupKey key. Any file that can be re-created or downloaded must be excluded from the backup. This is particularly important for large media files. If your application downloads video or audio files, make sure they are not included in the backup.

记得Documents/ and Application Support/目录下的文件默认会备份。通过使用NSURLIsExcludedFromBackupKey键调用-[NSURL setResourceValue:forKey:error:]可以从备份中排除文件。任何可以重新创建或下载的文件都必须排除在备份之外。这对于大型媒体文件来说尤其重要。如果你的应用下载视频或音频文件,确认他们不是包含在备份中。

Put temporary data in the tmp/ directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device. The system will periodically purge these files when your app is not running; therefore, you cannot rely on these files persisting after your app terminates.

将临时文件存储在tmp/目录。临时数据包含你不需要长期使用的任何数据。记得去删除这些文件当你已经使用完了,因此他们不会继续占用用户设备的空间。当应用不再运行时系统将会定时清除这些文件;因此当应用终止时你不能依赖这些文件持久化

Put data cache files in the Library/Caches/ directory. Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file. Generally speaking, the application does not require cache data to operate properly, but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transient, downloadable content. Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed.

将数据缓存文件存储在Library/Caches/ 目录。缓存数据可以应用于任何需要比临时文件持久化的数据,但是没有支持文件更久。一般而言,应用不会要求缓存数据也能正确操作,但它可以使用缓存数据来提升性能。缓存数据的例子包含(但不限于)数据库缓存文件和临时的、可下载的内容。系统会删除Caches/目录清空磁盘空间,因此当需要这些文件的时候你的应用必须可以重新创建或下载。

三 总结

1、存储内容类型

/Documents一般会存储用户生成的内容,此目录应该只包含您可能希望向用户公开的文件

/Library/ 一般不会存储用户数据文件,此目录包含不希望向用户公开的任何文件

/tmp/  一般会存储在应用程序启动期间不需要持久保存的临时文件,此目录会在应用不再运行时被清空

2、文件备份情况

/Documents这个目录的内容由iTunes和iCloud备份

/Library/        这个目录的内容由iTunes和iCloud备份

/tmp/            这个目录的内容没有iTunes或iCloud备份

四 目录说明

Table 1-1 lists some of the more important subdirectories inside the sandbox directory and describes their intended usage. This table also describes any additional access restrictions for each subdirectory and points out whether the directory’s contents are backed up by iTunes and iCloud.

表格1-1列出了一些在沙盒目录更重要的子目录并描述了它们的预期用途。这个表格也描述了对于每个子目录的任何额外访问限制,同时指出了目录内容是否从iTunes和iCloud中备份的。

Directory / Description

AppName.app

This is the app’s bundle. This directory contains the app and all of its resources.You cannot write to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle. For more information, see the Resource Programming Guide.The contents of this directory are not backed up by iTunes or iCloud. However, iTunes does perform an initial sync of any apps purchased from the App Store.

Documents/

Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, this directory should only contain files that you may wish to expose to the user.The contents of this directory are backed up by iTunes and iCloud.

Documents/Inbox

Use this directory to access files that your app was asked to open by outside entities. Specifically, the Mail program places email attachments associated with your app in this directory. Document interaction controllers may also place files in it.Your app can read and delete files in this directory but cannot create new files or write to existing files. If the user tries to edit a file in this directory, your app must silently move it out of the directory before making any changes.The contents of this directory are backed up by iTunes and iCloud.

Library/

This is the top-level directory for any files that are not user data files. You typically put files in one of several standard subdirectories. iOS apps commonly use the Application Support and Caches subdirectories; however, you can create custom subdirectories.Use the Library subdirectories for any files you don’t want exposed to the user. Your app should not use these directories for user data files.The contents of the Library directory (with the exception of the Caches subdirectory) are backed up by iTunes and iCloud.For additional information about the Library directory and its commonly used subdirectories, see The Library Directory Stores App-Specific Files.

tmp/

Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when they are no longer needed; however, the system may purge this directory when your app is not running.The contents of this directory are not backed up by iTunes or iCloud.


五  文档相关

文档参考  https://developer.apple.com

如对读书、测试感兴趣的小伙伴可关注如下WeChat:琦哥私房菜,小编会分享读书笔记和测试技术文章,希望同大家一起进步。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,634评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,951评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,427评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,770评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,835评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,799评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,768评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,544评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,979评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,271评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,427评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,121评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,756评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,375评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,579评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,410评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,315评论 2 352

推荐阅读更多精彩内容