iPhone - 存储数据的不同方式,优点和缺点 [英] iPhone - different ways to store data, advantages and disadvantages

查看:27
本文介绍了iPhone - 存储数据的不同方式,优点和缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否解释一下在 iPhone 上存储数据的不同方式,以及每种方式的优缺点.

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.

我已经阅读了很多关于 UserDefaults、CoreData、XML、plist 的内容......我有点迷茫.

I've read many things about UserDefaults, CoreData, XML, plist, ... and I'm a little bit lost.

现在,我明白了:

  • UserDefault 用于偏好,即使可以完成(少量数据),也不用于其他任何事情.它会生成一个 plist 文件,以后可以轻松地人工读取/检查到 XCode 中.

  • UserDefault is for preferences, and is not intended for anything else even if it can be done (small ammount of datas). It generates a plist file that can be easily humanly read/checked later into XCode.

XML 适用于结构化文本,但不适用于二进制数据.而且它易于编写,但不易于阅读.它会生成一个 XML 文件,以后可以轻松地在 XCode 中进行人工读取/检查.

XML is good for structured text, but not for binary datas. 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.

CoreData 功能强大,可以保存任何大小的任何内容,但包含起来有点难/长.并且已经编写的人工读取的核心数据是困难的"(可能吗?)

CoreData is powerfull, can save anything of any size, but is a little bit hard/long to include. And humanly read coredatas already written is "hard" (possible ?)

推荐答案

如果您想在文件系统上管理自己的存储,Plist 文件是另一种选择.NSArray 和 NSDictionary 提供了在 plist 文件中写入和读取这些集合的方法,只要您可以将所有数据存储在一种受支持的 plist 数据类型中.请参阅 属性列表编程指南了解详情.如果您可以轻松地将数据分解为不同的文件并且总是希望一次加载整个文件,这可能是一个不错的选择.

Plist files are another option if you want to manage your own storage on the file system. 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. 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 是一个强大的工具,尤其是当您想要存储对象图时.当您希望能够轻松存储和加载模型对象时,这可能是一个合适的选择.

CoreData is a powerful tool, especially if you want to store a graph of objects. It might be an appropriate choice when you want to be able to store and load model objects easily.

如果您想存储关系数据并针对它运行查询,SQLite 非常棒.如果您想要快速高效的查询但不需要将结果转换为模型对象(或有某些原因编写自己的 ORM 层),这可能是一个不错的选择.

SQLite is great if you want to store relational data and run queries against it. 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).

正如您所提到的,NSUserDefaults 是一种用于存储用户凭据的便捷工具,但不适用于大量数据.它还允许您在设置应用程序中公开设置,以便用户可以在一个公共位置设置应用程序行为,而无需启动您的应用程序.

As you mentioned NSUserDefaults is a convenient tool for storing user credentials but is not intended for larger volumes of data. 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.

如果您想通过文件共享设置向用户公开这些文件,任何形式的基于文件的存储都可能具有额外的价值,允许应用程序数据在同步到 PC 时出现在 iTunes 文档目录中.

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.

无论您使用哪种存储机制,这些选项中的每一个都要求您为数据管理某种架构.

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 支持将数据从一种模式迁移到另一种模式,但在所有情况下都需要开发人员的工作、意识和测试.

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.

这篇关于iPhone - 存储数据的不同方式,优点和缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆