Java 在 Realm Android 上 List<Object> 或 RealmList<RealmObject>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30097810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List<Object> Or RealmList<RealmObject> on Realm Android
提问by Bachlet Tansime
I need a list<Object>
using Realm. I tried RealmList<RealmObject>
but it doesn't work because RealmObject
is abstract.
我需要一个list<Object>
使用领域。我试过了,RealmList<RealmObject>
但它不起作用,因为它RealmObject
是抽象的。
采纳答案by Christian Melchior
Christian from Realm here. You can only save objects that extend RealmObject inside a Realm. This is because Realm is not a schemaless database. We do require a schema and that schema is defined by your objects that extend RealmObject. We use RealmList because it abstracts away the communication with the underlying core database, but it implements the List interface.
来自 Realm 的 Christian。您只能在 Realm 中保存扩展 RealmObject 的对象。这是因为 Realm 不是无模式数据库。我们确实需要一个架构,并且该架构由您扩展 RealmObject 的对象定义。我们使用 RealmList 是因为它抽象了与底层核心数据库的通信,但它实现了 List 接口。
This means that
这意味着
public class Foo extends RealmObject {
private List<Object> objects; // not legal
private RealmList<Object> objects; // not legal
private RealmList<RealmObject> objects; // not legal
}
public class Foo extends RealmObject {
private RealmList<Foo> objects; // legal
}
List<Foo> reference = foo.getObjects(); // Legal