问题描述
我需要使用Realm的列表< Object>
。我尝试了 RealmList< RealmObject>
但它不起作用,因为 RealmObject
是抽象的。
I need a list<Object>
using Realm. I tried RealmList<RealmObject>
but it doesn't work because RealmObject
is abstract.
推荐答案
来自Realm的Christian。您只能保存在Realm中扩展RealmObject的对象。这是因为Realm不是无模式数据库。我们确实需要一个模式,该模式由扩展RealmObject的对象定义。我们使用RealmList,因为它抽象了与底层核心数据库的通信,但它实现了List接口。
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.
这意味着
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
这篇关于列表与LT;对象>或RealmList< RealmObject>在Realm Android上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!