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

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

问题描述

我认为这已经涵盖了,但我的搜索没有返回任何相关内容.

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

我知道有 NSUserDefaults、Core Data、对象归档、原始 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 服务器和 Core Data 的用法是显而易见的,但是 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.

钥匙串(见 SSKeychain 用于出色的包装器):用于存储敏感数据,例如凭据.

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:

  • 用户生成的内容存储:一个简单的地理点列表,将通过地图或列表显示.
  • 向您的应用提供简单的初始数据:在这种情况下,plist 将包含在 NSBundle 中,而不是由用户生成并由用户数据填充.
  • 分离所需的数据来自其他数据的应用程序的特定模块.例如,构建分步启动教程所需的数据,其中每一步都与其他步骤相似,但只需要不同的数据.对这些数据进行硬编码很容易填满您的代码,因此您可以成为更好的开发人员,并使用 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.

Core Data 功能强大,可以支持不同的持久化存储(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).例如,对于我工作的一个项目,网络服务为我提供了一些大型 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 storage 这应该是显而易见的:如果您需要将数据存储到服务器,那是因为设备不应该是该数据的唯一所有者.但是,如果您只需要在不同的 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天全站免登陆