List<Object> Or RealmList on Re

2020-03-09 08:45发布

I need a list<Object> using Realm. I tried RealmList<RealmObject> but it doesn't work because RealmObject is abstract.

1条回答
对你真心纯属浪费
2楼-- · 2020-03-09 08:50

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.

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
查看更多
登录 后发表回答