NSKeyedArchiver 仅对数组的一部分进行编码
我有一个有时会更改的对象列表,并且我希望每当应用程序关闭或移动到后台时都在设备上保留持久缓存。 列表中的大多数对象都不会改变,所以我想知道保存列表的最佳方法是什么。我考虑了两个主要选项:
使用 NSKeyedArchiver / unArchiver - 这是最方便的方法,因为我正在序列化的对象包含其他自定义对象,所以这样我就可以为每个对象编写一个自定义编码方法他们。主要问题是我没有在 Google 上找到如何仅序列化已更改的对象,并且每次都序列化整个列表似乎非常浪费。
使用 SQLite - 这就是我当前正在使用的,这里最糟糕的问题是添加\更改对象的属性非常复杂,而且不太优雅。
有什么方法可以让我享受 NSKeyedArchiver 的便利,但只序列化更改的对象?
I have a list of objects that can sometimes change, and I want to keep a persistent cache on the device whenever the app is closed or move to the background.
Most of the objects in the list will not change, so i was wondering what is the best way to save the list. I have two major options i think about:
Using NSKeyedArchiver / unArchiver - This is the most convenient method, because the objects i'm serializing hold other custom objects, so this way i can just write a custom encode method for each of them. The major problem is that i didn't find on Google how to serialize only the changed objects, and serializing the entire list every time seems very wasteful.
Using SQLite - this is what i'm currently using, and the worst problem here is that adding \ changing properties of the objects is very complicated, and much less elegant.
Is there any way that i can enjoy the convenience of NSKeyedArchiver but only serialize the changed objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像 Adam Ko 一样,我建议使用 Core Data:
毕竟这种问题才是它真正擅长的!
如果您的缓存项是相互独立的,则可以通过简单地包装您的缓存项来实现
NSManagedObject
的薄层(即您可以从Core Data中受益仅对您的应用程序进行微小更改)。该包装器实体可以将缓存项的存档版本存储在 NSBinaryDataAttributeType 类型的属性中,并通过瞬态属性提供对未存档对象的访问。
请参阅 非标准持久属性作为示例。
Like Adam Ko I would suggest using Core Data:
This kind of problem is what it's really good at, after all!
If your cache items are independent from each other, this could be achieved by simply wrapping your cache-items by a thin layer of
NSManagedObject
(i.e. you could benefit from Core Data with only minor changes to your app).This wrapper entity could store an archived version of a cache item in an attribute of type
NSBinaryDataAttributeType
and provide access to the unarchived object through a transient property.See Non-Standard Persistent Attributes for an example.