在Realm中存储一个简单的数组,而无需在Swift中创建托管列表(Storing a simple array in Realm without creating a managed List in

编程入门 行业动态 更新时间:2024-10-27 08:26:02
在Realm中存储一个简单的数组,而无需在Swift中创建托管列表(Storing a simple array in Realm without creating a managed List in Swift)

我有一组数据,不需要成为托管列表,这意味着我不需要Realm为具有链接的项目创建新模型,并且能够查询项目等。我只是想要一个简单的数组,通常是不会从Object继承的基元,这些基元将被我的主对象持久化。

我能想到的唯一解决方案是使用NSData和NSKeyedArchiver / NSKeyedUnarchiver。 这是最好的/唯一的方法来做到这一点? 即使我不认为我需要它,我也应该使用List吗?这种情况的最佳做法是什么?

I have an array of data that doesn't need to be a managed List, meaning I don't need Realm to create a new model for the items with links and the ability to query on the items, etc. I just want a simple array, typically of primitives that don't inherit from Object anyway, that will be persisted with my main object.

The only solution I can think of is to use NSData and NSKeyedArchiver/NSKeyedUnarchiver. Is that the best/only way to do this? Should I just use List even if I don't think I'll need it — what's the best practice for this situation?

最满意答案

Realm不支持原始数组(尽管该功能即将推出),所以最直接的解决方案是使用充满模型对象的List来包装原始数据。 如果您觉得更适合您的特定用例,那么将NSData存档并从NSData归档并将数据存储在Realm模型中没有任何问题。

Realm doesn't support arrays of primitives (although that functionality is coming soon), so the most straightforward solution is to use a List filled with model objects that just wrap your primitives. There's nothing wrong with archiving to and from NSData and storing the data in your Realm model, though, if you feel that better suits your particular use case.

更多推荐

本文发布于:2023-07-16 12:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128523.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   简单   列表   Realm   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!