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

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

问题描述

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



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

我现在迷路了。
任何主体可以解释MAIN功能之间的区别是什么?

解决方案

保存以及是仅在内部使用它还是必须与外部服务交换数据。



NSCoding通常是一个数据串行器。许多内置对象实现NSCoder协议,允许您将它们保存为二进制流(文件,在sqlite的BLOB等)NSKeyedArchiver提供了基于字符串标签在这些流中搜索的加号,有点像字典,但你只能使用字符串作为键。



但是,如果你有个相同的对象,那么这个方法是很好的。 类,你最好去使用数据库方法,SQLite或CoreData。 CoreData实际上是一个围绕SQLite的包装,它简化了很多设计数据模型,并且在窗帘后面对数据库进行查询,而不需要编写SQL语句。在CoreData中,您可以定义类,并且类的每个实例都可以持久化,也就是说,您可以获取对象成员的值,而不必总是在内存中。这是一种非常方便的方式来存储大量的结构化数据。例如,如果您要编写网络浏览器,则可以使用名称,网址或者上次访问时间存储用户的书签。



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



如果您自己设计数据模型,指向使用plists,但也许有人会纠正我。



编辑:我在这里添加了一个关于如何使用 NSCoding Core Data ,作为奖励, SQLite



UPDATE 12.01 .2016:如果您正在寻找持久性解决方案,我建议您也查看境界


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

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 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.

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.

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.

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

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

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

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

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