NSKeyedArchiver 仅对数组的一部分进行编码

发布于 2024-10-08 11:49:39 字数 398 浏览 0 评论 0原文

我有一个有时会更改的对象列表,并且我希望每当应用程序关闭或移动到后台时都在设备上保留持久缓存。 列表中的大多数对象都不会改变,所以我想知道保存列表的最佳方法是什么。我考虑了两个主要选项:

  1. 使用 NSKeyedArchiver / unArchiver - 这是最方便的方法,因为我正在序列化的对象包含其他自定义对象,所以这样我就可以为每个对象编写一个自定义编码方法他们。主要问题是我没有在 Google 上找到如何仅序列化已更改的对象,并且每次都序列化整个列表似乎非常浪费。

  2. 使用 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:

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

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-10-15 11:49:39

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文