NSCoding VS 核心数据 [英] NSCoding VS Core data

查看:14
本文介绍了NSCoding VS 核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一篇文章来解释 NSCoding (NSKeyedArchiver...) 相对于使用 CoreData (SQLite....) 的优缺点.

I've been searching for an article that explains NSCoding (NSKeyedArchiver...) advantages and disadvantages over use of CoreData (SQLite....).

有很多选择,我可以实现我自己的自定义二进制读写器,或者使用 plists/xml/json...或者使用 SQLite 或 NSCoding.

There's a lot of options, I can implement my own custom binary reader/writer, or use plists/xml/json... or use SQLite or NSCoding.

我现在有点迷茫.任何机构都可以解释主要功能之间的区别是什么?

I'm kind of lost right now. Can any body explain what's the difference between the MAIN features?

推荐答案

这取决于您要保存哪种数据,以及您是仅在内部使用它还是必须与外部服务交换数据.

It depends which kind of data you want to save and whether you will use it only internally or you have to exchange the data with an external service.

NSCoding 通常来说是一个数据序列化器.许多内置对象实现了 NSCoder 协议,允许您将它们保存为二进制流(文件、sqlite 的 BLOB 等). NSKeyedArchiver 为您提供了基于字符串标签在此类流中搜索的优势,有点像字典,但你只能使用字符串作为键.如果您偶尔需要持久化一些不同类的对象,这种方法是很好的.

NSCoding is generally speaking a data serializer. A lot of built-in objects implements the NSCoder protocol that allows you to save them as a binary stream (file, in a BLOB of an sqlite, etc.) The NSKeyedArchiver gives you the plus of searching in such streams based on a string label, a bit like a dictionary but you can use only strings as keys. This approach is good if you occasionally have to persist some objects of different classes.

但是,如果您有 许多 相同 类的对象,您最好选择数据库方法,SQLite 或 CoreData.CoreData 实际上是 SQLite 的包装器,它简化了数据模型的设计,并且可以在幕后对 DB 进行查询,而无需编写 SQL 语句.在 CoreData 中,您定义了您的类,并且该类的每个实例都可以持久化,即您可以取回对象成员的值,而无需将它们始终保留在内存中.这是一种非常方便的存储大量结构化数据的方式.例如,如果您要编写一个 Web 浏览器,则可以存储用户的书签以及名称、URL 和上次访问时间.

However if you have many objects of the same class, you'll better go for a database-approach, SQLite or CoreData. CoreData is practically a wrapper around SQLite that eases a lot designing your data model, and does the queries to the DB behind the curtains, without the need of writing SQL statements. In CoreData you define your classes, and each instance of the class can be persisted i.e. you can get back the values of the members of the object without having them always in the memory. This is a very convenient way to store a lot of structured data. For example if you'd write a web browser, you could store the bookmarks of the user with the name, URL, and maybe last visited time.

对于 XML 和 JSON,如果您仅在设备本地使用数据,则没有特别的优势.如果您必须与某些外部服务进行通信,您可能会考虑缓存/保存 XML/JSON 对象以供以后使用.其他方法是每次需要时从内部数据结构(见上文)重新生成这些数据.

For XML and JSON there're no particular advantage if you use the data only locally to the device. If you have to communicate with some external service, you might consider to cache/save the XML / JSON objects as they are for later use. Other approach would be to regenerate this data from your internal data structures (see above) every time you need it.

如果您自己设计数据模型,我认为使用 plist 的意义就更小了,但也许有人会纠正我.

If you design your data model yourself, I see even less point to use plists, but maybe somebody will correct me.

我在这里添加了一个关于如何使用 NSCoding, 核心数据,作为奖励,SQLite.

I add here a short link reference for tutorials on how to use NSCoding, Core Data, and as a bonus, SQLite.

更新 12.01.2016:如果您正在寻找持久性解决方案,我建议您也查看 Realm.

UPDATE 12.01.2016: If you are looking for persistence solutions I suggest you to also check out Realm.

这篇关于NSCoding VS 核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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