RealmObject中的RealmList.如何坚持订单?

Wei*_*zhi 3 android realm realm-list

我有一个RealmObjectRealmList它的领域为一体.

public class ToyMaker extends RealmObject {
    private RealmList<Toy> toyList;
    ...
}

public class Toy extends RealmObject {
    ...
}
Run Code Online (Sandbox Code Playgroud)
  1. 写入Realm并将其读回时Toy,toyList是否会保留in 的顺序?

  2. 如果Realm没有持续订购,那么我如何手动实现这一点

    答:我无法在Toy类中添加其他字段(例如索引)(Toy可能有很多ToyMaker字段,因此索引字段Toy不可行).

    B.订单按Toy添加的顺序排列toyList.

Chr*_*ior 6

A RealmList是有序的,所以情况#1是正确的.只需按照您想要的顺序添加它们,该订单将被保留.

但一般情况下,订购并不能保证,因此在Realm中创建2个对象并不意味着订单:

realm.createObject(Person.class, 1);
realm.createObject(Person.class, 2);

// Doing this is not guaranteed to return Person:1
realm.where(Person.class).findAll().first()

// Always use a sort in queries if you want a specific order
realm.where(Person.class).findAllSorted("id", Sort.ASCENDING).first();
Run Code Online (Sandbox Code Playgroud)