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

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

问题描述

你能解释一下我在iPhone上存储数据的不同方法,对于doig的每种方式,这都是优点和缺点。



我'读了很多关于UserDefaults,CoreData,XML,plist,...和我有点失去了。



现在,我明白了: p>


  • UserDefault适用于首选项,不适用于任何其他操作,即使可以完成(小数据量)。


  • XML适用于结构化文本,但不适用于二进制数据。


  • CoreData是强大的,可以保存任何大小的任何东西,但有点硬/长包括。



解决方案

Plist文件是另一个选项,如果你想在文件系统上管理自己的存储。 NSArray和NSDictionary提供了用于向plist文件中写入和读取这些集合的方法,只要您可以将所有数据存储在一个支持的plist数据类型中。请参阅属性列表编程指南



CoreData是一个强大的工具,特别是如果你想存储一个对象图。



如果你想存储关系数据并对它进行查询,SQLite是一个很好的选择。 。如果你想要快速和高效的查询,但不需要将结果转换为模型对象(或有一些理由写自己的ORM层),这可能是一个不错的选择。



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



任何形式的基于文件的存储可能如果要通过文件共享设置将这些文件公开给用户,则允许应用程序数据在与PC同步时出现在iTunes Documents目录中。






无论您使用每个选项的存储机制如何,您都需要为数据管理某种架构。



您需要跟踪您的数据存储在您的应用程序的每个版本的格式。任何时候你改变你对保存数据格式的期望,你需要支持旧版本。我看到太多的应用程式在更新后崩溃,因为它们不处理旧版应用程式所储存的资料,或假设使用者安装并执行每个版本的应用程式,而不是略过更新。



CoreData对于将数据从一个模式迁移到另一个模式提供了一些支持,但它在所有情况下都需要开发人员的工作,意识和测试。


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.

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

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). It generates a plist file that can be easily humanly read/checked later into 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 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 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 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 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).

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.

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 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天全站免登陆