我什么时候应该使用iOS中的各种存储机制? [英] When should I use the various storage mechanisms in iOS?

查看:144
本文介绍了我什么时候应该使用iOS中的各种存储机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这已经被覆盖了,但我的搜索没有任何相关性。

I thought this would be covered already, but my search returned nothing of relevance.

我知道有NSUserDefaults,核心数据,对象存档,原始SQLite,plist,当然还有Web服务器存储。对于初学者来说,不清楚和有些模糊的是何时使用这些各种工具。

I am aware that there is NSUserDefaults, Core Data, object archiving, raw SQLite, plists, and of course, storage by web servers. What is unclear and somewhat hazy to a beginner is when to employ each of these various tools.

Web服务器与核心数据的使用是显而易见的,但NSUserDefaults与plists有什么关系呢?
核心数据与对象存档?一个简单的用例细分真的可以帮助我理解为什么在iOS中有这么多的存储选项。

The usages of web servers vs Core Data is obvious, but what about NSUserDefaults vs plists? Core Data vs object archiving? A simple breakdown of use cases would really help me understand why there are so many options for storage in iOS.

推荐答案

我尝试写一个快速简单的常见用例列表,因为正如@rmaddy所说,这个答案可以填写一章书:

I try to write a quick and simple list of common use cases, because as @rmaddy says this answer could fill a book chapter:


  • NSUserDefaults :存储简单的用户首选项,没有太复杂或安全。如果您的应用有一个带有几个开关的设置页面,您可以在此处保存数据。

  • NSUserDefaults: stores simple user preferences, nothing too complex or secure. If your app has a setting page with a few switches, you could save the data here.

Keychain (参见 SSKeychain wrapper):用于存储敏感数据,如凭据。

Keychain (see SSKeychain for a great wrapper): used to store sensitive data, like credentials.

PLists :用于存储更大的结构化数据(但不是很大):它非常灵活格式,可以在很多场景中使用。一些示例是:

PLists: used to store larger structured data (but not huge): it is a really flexible format and can be used in a great number of scenarios. Some examples are:


  • 用户生成的内容存储:将由地图或列表显示的简单Geopoint列表。

  • 为您的应用提供简单的初始数据:在这种情况下,plist将包含在NSBundle中,而不是由用户生成并由用户数据填充。

  • 将应用程序的
    特定模块所需的数据与其他数据分开。例如,
    构建逐步启动教程所需的数据,其中每个步骤与其他步骤类似,但只需要不同的数据。对这些数据进行硬编码很容易填充你的代码,因此你可以成为一个更好的开发人员,并使用plist来存储数据并从中读取数据。

  • 你正在编写一个库或框架,可以由使用它的开发人员以某种
    的方式配置。

  • User generated content storage: a simple list of Geopoint that will be shown by a map or list.
  • Provide simple initial data to your app: in this case the plist will be included in the NSBundle, instead of being generated by user and filled by user data.
  • Separate the data needed for a particular module of your application from other data. For example, the data needed to build a step-by-step startup tutorial, where each step is similar to the others but just needs different data. Hard-coding this data would easily fill your code, so you could be a better developer and use plists to store the data and read from them instead.
  • You are writing a library or framework that could be configured in some way by the developer that uses it.

Object归档可能有助于序列化更复杂的对象,可能包含二进制数据,这些对象不能(或者您不希望)映射到更简单的结构(如plist)上。

Object archiving could be useful to serialize more complex objects, maybe full of binary data, that can't (or that you don't want to) be mapped on simpler structures like plists.

核心数据功能强大,可以由不同的持久存储支持(SQLite只是其中之一,但你也可以选择XML文件,或者你甚至可以编写自己的格式!),并给出元素之间的关系。它很复杂,并提供许多对开发有用的功能,如KVO和上下文。您应该将它用于许多相关记录的大型数据集,这些记录可以由用户生成或由服务器提供。

Core Data is powerful, can be backed by different persistent stores (SQLite is just one of them, but you can also choose XML files or you can even write your own format!), and gives relationships between elements. It is complex and provides many features useful for the development, like KVO and contexts. You should use it for large data sets of many correlated records, that could be user generated or provided by a server.

Raw SQLite 非常有用,当您需要真正,快速地访问关系
数据源时(Core Data引入了一些开销),或者如果您需要跨多个平台支持相同的SQLite格式(您应该永远不要乱用CoreData内部SQLite:它使用自己的格式,因此您不能只导入CoreData中的现有SQLite)。例如,对于我工作的项目,web服务为我提供了一些大型SQLite而不是jsons或xmls:这些SQLite中的一些被导入到CoreData(操作可能需要一段时间,具体取决于源大小),因为我需要所有它的功能,而其他SQLite直接读取,以实现快速访问。

Raw SQLite is useful when you need really, really fast access to a relational data source (Core Data introduces some overhead), or if you need to support the same SQLite format across multiple platforms (you should never mess with CoreData inner SQLite: it uses its own format, so you can't just "import" an existing SQLite in CoreData). For example, for a project I worked for, a webservice provided me some large SQLite instead of jsons or xmls: some of this SQLite were imported to CoreData (operation that could take a while, depending on the source size), because I needed all the features of it, while other SQLites were read directly for a really fast access.

Webserver存储它应该是显而易见的:如果您需要将数据存储到服务器,那是因为该设备不应该是该数据的唯一所有者。但是,如果你只需要在不同的iOS设备上同步相同的应用程序(甚至是Mac端口版本的应用程序),你也可以看看iCloud存储。显然。

Webserver storage well it should be obvious: if you need to store data to a server it is because the device shouldn't be the only owner of that data. But if you just need to synchronize the same App across different iOS devices (or even with a Mac-ported version of the App) you could also look at iCloud storage, obviously.

这篇关于我什么时候应该使用iOS中的各种存储机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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