简体   繁体   English

iPhone - 存储数据的不同方式,优点和缺点

[英]iPhone - different ways to store data, advantages and disadvantages

Could you explain me which are the different ways to store datas on the iPhone, and for each way of doig this, which are the advantages and disadvantages. 你能解释一下在iPhone上存储数据的不同方法,以及每种方式,这都是优点和缺点。

I've read many things about UserDefaults, CoreData, XML, plist, ... and I'm a little bit lost. 我读过很多关于UserDefaults,CoreData,XML,plist等的东西,而且我有点迷失了。

For now, I understand that : 现在,我理解:

  • UserDefault is for preferences, and is not intended for anything else even if it can be done (small ammount of datas). UserDefault用于首选项,即使可以完成,也不用于其他任何内容(小数据量)。 It generates a plist file that can be easily humanly read/checked later into XCode. 它生成一个plist文件,可以在以后轻松地人工读取/检查到XCode。

  • XML is good for structured text, but not for binary datas. XML适用于结构化文本,但不适用于二进制数据。 And it's easy to write, but not to read.It generates an XML file that can be easily humanly read/checked later into XCode. 而且它很容易编写,但不能读取。它生成的XML文件可以在以后轻松地人工读取/检查到XCode中。

  • CoreData is powerfull, can save anything of any size, but is a little bit hard/long to include. CoreData功能强大,可以保存任何大小的任何内容,但包含的内容有点难/长。 And humanly read coredatas already written is "hard" (possible ?) 人文阅读已经写好的coredatas是“硬”(可能吗?)

SQLite DB - DB Used by iOS development, you can interact with this directly depending on your preferences (eg you want to use SQL statements). SQLite DB - DB由iOS开发使用,您可以根据自己的喜好直接与此进行交互(例如,您希望使用SQL语句)。

CoreData - Abstraction to SQLite DB so you can remove SQL statements and use the API instead. CoreData - 对SQLite DB的抽象,因此您可以删除SQL语句并使用API​​。 Advantage of this is its compatibility with the Cocoa API. 这样做的好处是它与Cocoa API的兼容性。 In our production applications we use CoreData over SQLite. 在我们的生产应用程序中,我们使用CoreData而非SQLite。

File System - You can store files directly here and use a convention. 文件系统 - 您可以直接在此处存储文件并使用约定。 You might also want to read about using the cache folder iOS development for temporary data. 您可能还想了解如何使用缓存文件夹iOS开发来获取临时数据。

XML - Case to case. XML - 案例分析。 In commercial application, we only use XML for interfacing between systems. 在商业应用中,我们只使用XML来连接系统。 Eg iPad to Cloud server. 例如iPad到云服务器。

UserDefault - only for parameters UserDefault - 仅用于参数

Plist files are another option if you want to manage your own storage on the file system. 如果要在文件系统上管理自己的存储,则Plist文件是另一种选择。 NSArray and NSDictionary provide methods for writing and reading those collections to and from plist files as long as you can store all of your data in one of the supported plist data types. NSArray和NSDictionary提供了在plist文件中写入和读取这些集合的方法,只要您可以将所有数据存储在一个受支持的plist数据类型中。 See the Property List Programming Guide for details. 有关详细信息,请参见属性列表编程指南 It might be a good option if you can easily break up your data into distinct files and always want to load an entire file at once. 如果您可以轻松地将数据分解为不同的文件并且总是希望一次加载整个文件,那么这可能是一个不错的选择。

CoreData is a powerful tool, especially if you want to store a graph of objects. CoreData是一个功能强大的工具,特别是如果您想存储对象图形。 It might be an appropriate choice when you want to be able to store and load model objects easily. 当您希望能够轻松存储和加载模型对象时,它可能是一个合适的选择。

SQLite is great if you want to store relational data and run queries against it. 如果要存储关系数据并对其运行查询,SQLite非常有用。 It might be a good choice if you want fast and efficient queries but don't need to convert the results into model objects (or have some reason for writing your own ORM layer). 如果您想要快速有效的查询但不需要将结果转换为模型对象(或者有一些编写自己的ORM层的原因),那么这可能是一个不错的选择。

As you mentioned NSUserDefaults is a convenient tool for storing user credentials but is not intended for larger volumes of data. 正如您所提到的,NSUserDefaults是一种用于存储用户凭据的便捷工具,但不适用于大量数据。 It also allows you to expose settings in the settings app so that a user can set application behavior in one common location without launching your app. 它还允许您在设置应用程序中公开设置,以便用户可以在一个公共位置设置应用程序行为,而无需启动您的应用程序。

Any form of file based storage may have additional value if you want to expose those files to the user through the File Sharing settings, allowing application data to appear in the iTunes Documents directory when synching to a PC. 如果要通过“文件共享”设置将这些文件公开给用户,则任何形式的基于文件的存储都可能具有附加值,从而允许应用程序数据在同步到PC时显示在iTunes文档目录中。


Regardless of the storage mechanism you use every one of these options requires that you manage some sort of schema for your data. 无论您使用这些选项中的每一个存储机制,都需要为数据管理某种模式。

You need track of the format your data is stored in in every version of your application. 您需要跟踪每个版本的应用程序中存储数据的格式。 Any time you change your expectation of the format of saved data you need to support old versions. 只要您改变对保存数据格式的期望,就需要支持旧版本。 I see too many apps crash after an update because they do not handle data saved by old versions of the app or assume that users will have installed and run every version of the app instead of skipping some updates. 我发现更新后有太多应用程序崩溃,因为它们不处理旧版本应用程序保存的数据,或者假设用户已安装并运行应用程序的每个版本而不是跳过某些更新。

CoreData has some support for migrating data from one schema to another but it is something that requires developer work, awareness, and testing in all cases. CoreData支持将数据从一个模式迁移到另一个模式,但在所有情况下都需要开发人员的工作,意识和测试。

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

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