领域或更新的领域顺序

时间:2017-01-31 09:31:43

标签: android realm

我正在使用 Realm 来保留 Android 项目的对象。 问题是我有许多类和内部字段的组合作为类和列表。 像

这样的东西
class A extends RealmObject{
   @PrimaryKey long id;
   B b;
} 

class B extends RealmObject{
   @PrimaryKey long id;
   RealmList<C> cList;
}

class C extends RealmObject{
  @PrimaryKey long id;
  Date date;
  String value;
  //other fields....
}

问题是我想更新 C 的某些字段或在 RealmList of B 中插入新的 C 。在以什么顺序我应该这样做以保持关系?谁能举一个真实的例子?此外,Realm不支持ID的自动增量,目前我在启动时将它们设置为currentTimeInMillis。有更好的替代方案吗?

2 个答案:

答案 0 :(得分:6)

让您的模型类

class A extends RealmObject{
   @PrimaryKey
   private long id;
   private B b;

   public A(){
       //required empty constructor
   }

   public A(long id){
       this.id = id;
   }

   public B getB(){
       return b;
   }

   public void setB(B b){
       this.b = b;
   }
} 

class B extends RealmObject{
   @PrimaryKey
   private long id;
   private RealmList<C> cList = new RealmList<>();

   public B(){
       //required empty constructor
   }

   public B(long id){
   this.id = id;
   }

   public RealmList<C> getCList(){
       return cList;
   }

   public void setCList(RealmList<C> cList){
       this.cList = cList;
   }
}

class C extends RealmObject{
  @PrimaryKey 
  private long id;
  private Date date;
  private String value;
  //other fields....

  public C(){
      //required empty constructor
  }

  public C(long id){
      this.id = id;
  }
}

示例 - 1:创建新对象并根据层次结构分配

    Realm realm = Realm.getDefaultInstance();
    realm.beginTransaction();

    C c = new C(id);
    realm.insertOrUpdate(c);

    B b = new B(id);
    RealmList<C> list = b.getcList();
    list.add(c);
    realm.insertOrUpdate(b);

    A a = new A(id);
    a.setB(b);
    realm.insertOrUpdate(a);
    realm.commitTransaction();

示例 - 2:更新数据库中的现有条目

    C c = realm.where(C.class).equalTo("id", id).findFirst();
    realm.beginTransaction();
    c.setValue("New Value"); //setter method for value
    c.insertOrUpdate();
    //while updating the existing entry in the database, you need not worry about the hierarchy, Realm will maintain the same hierarchy for updates
    //If you want to copy the existing entry from one object to another, you can use combination of method-1 and method-2
    realm.commitTransaction();

答案 1 :(得分:4)

这将是这样的:

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        // using managed objects
        A a = realm.where(A.class).findFirst(aId);
        if(a == null) {
           a = realm.createObject(A.class, aId);
        }
        B b = realm.where(B.class).findFirst(bId);
        if(b == null) {
           b = realm.createObject(B.class, bId);
        }
        a.setB(b);

        // in case of managed objects
        for(List<Long> cId : cIds) {
            C c = realm.where(C.class).findFirst(cId);
            if(c == null) {
                c = realm.createObject(C.class, cId);
            }
            if(!b.getCs().contains(c)) { 
                b.getCs().add(c);
            }
        }

        // in case of unmanaged objects of C, adding to managed object B
        b.getCs().addAll(realm.copyToRealmOrUpdate(unmanagedCs));

      ////// adding new unmanaged objects to Realm
        A _a = new A();
        _a.setId(aId);
        realm.insertOrUpdate(_a);

        B _b = new B();
        _b.setId(bId);
        _b.setCs(new RealmList<C>());
        for(C _c : unmanagedCs) {
           _b.add(_c);
        }
        realm.insertOrUpdate(_b);
    }
});