2015-07-12 44 views
3

我正在使用Realm作爲我的android應用程序中的數據模塊,但我遇到了一些關於如何將自定義方法分配給Realm對象的問題。在iOS coredata模塊中,您有NSObjects,它可以保存到內存中,並且可以在各自的類中提供可擴展性。我可以用什麼樣的系統來解決這個問題?我嘗試爲我的所有領域對象製作「Parent Classes」,但這不起作用。RealmObjects ...解決方案中的自定義方法?

例如:「TeacherRealm」只包含教師對象的獲取者和設置者,而「TeacherParent」則允許用戶運行某些算法以找到教師的數據。在TeacherParent類中,我嘗試初始化一個TeacherRealm,並在TeacherRealm中爲TeacherParent對象提供了一個getter和setter。低和看,我的TeacherRealm對象類不支持自定義TeacherParent對象。

有沒有人遇到過這個問題/找到解決辦法了?我知道這聽起來令人困惑,但我可以在必要時提供更多信息

+1

爲什麼你不想將模型與在模型上執行操作的類分開?:confused: – Yaroslav

+1

你的TeacherRealm類的靜態方法怎麼樣?'public static RealmResults findGoodTeachers(Realm realm,int teacherAge)' – beeender

回答

1

我正在通過類似的問題。我的方法將是使用靜態'幫助'方法來實現所有額外的邏輯。在方法調用中傳遞我的對象類型的實例。

+0

是的,這就是我最終做的,它一直在努力 –

+0

k好的解決方法 – primax79

1

我有相同的情況來尋找realmObjects中自定義邏輯的解決方法。

我濫用建設者模式一個舒適的方式。


public class Information extends RealmObject { 
    @PrimaryKey 
    private String id; 
    private int value; 
    private String text; 

    //getter and setter for id, value and text 

    //Helps to create and commit a information realm object 
    public static class Create { 

     private Realm realm; 
     private Information information; 

     public Create() { 
      this.realm = Realm.getDefaultInstance(); 
      this.realm.beginTransaction(); 

      this.information = realm.createObject(Information.class); 

      //init realmObject with defauls 
      this.information.setId(UUID.randomUUID().toString()); 
      this.information.setValue(0); 
      this.information.setText(""); 
      //space for additional logic 
     } 
     public Create setValue(int val) { 
      this.information.setValue(val); 
      //space for additional logic 
      return this; 
     } 
     public Create setText(String s) { 
      this.information.setText(s); 
      //space for additional logic 
      return this; 
     } 
     public Information commit() { 
      //space for additional logic 
      this.realm.commitTransaction(); 
      return this.information; 
     } 
    } 

    //Helps to modify and commit a information realm object 
    public static class Modify { 

     private Realm realm; 
     private Information information; 

     public Modify(Information information) { 
      this.realm = Realm.getDefaultInstance(); 
      this.information = information; 
      this.realm.beginTransaction(); 
     } 

     public Modify setValue(int val) { 
      this.information.setValue(val); 
      //space for additional logic 
      return this; 
     } 

     public Modify setText(String s) { 
      this.information.setText(s); 
      //space for additional logic 
      return this; 
     } 

     public void commit() { 
      //space for additional logic 
      this.realm.commitTransaction(); 
     } 
    } 
} 

如何使用

//create a reamlobject 
Information info = new Information.Create() 
        .setText("Hello World"); 
        .commit(); 

//modify the created info object 
new Information.Modify(info) 
       .setValue(1) 
       .commit(); 
7

編輯:Realm 0.88.0 has enabled the usage of custom methods,並且還可以實現你的領域對象的接口。但是我保留了我的存儲庫模式,這是以下原始答案。撰寫本文時的最新版本是0.88.1。


我堅信data對象應該只包含數據和邏輯應該分開。

public interface RealmRepository<T extends RealmObject, ID extends Serializable> { 
    T findOne(Realm realm, ID id); 

    RealmResults<T> findAll(Realm realm); 

    void insertOrUpdate(Realm realm, T t); 

    void insertOrUpdate(Realm realm, Collection<T> t); 

    T saveOrUpdate(Realm realm, T t); 

    RealmList<T> saveOrUpdate(Realm realm, RealmList<T> tList); 

    RealmQuery<T> query(Realm realm); 

    void delete(Realm realm, ID id); 

    void delete(Realm realm, T t); 

    void deleteAll(Realm realm, RealmResults<T> realmResults); 

    void deleteEveryObject(Realm realm); 

    long count(Realm realm); 
} 

而且

public abstract class BaseRealmRepositoryImpl<T extends RealmObject, ID extends Serializable> 
     implements RealmRepository<T, ID> { 
    protected Class<T> clazz; 

    public BaseRealmRepositoryImpl(Class<T> clazz) { 
     this.clazz = clazz; 
    } 

    @Override 
    public RealmResults<T> findAll(Realm realm) { 
     return query().findAll(); 
    } 

    @Override 
    public void insertOrUpdate(Realm realm, T t) { 
     realm.insertOrUpdate(t); 
    } 

    @Override 
    public void insertOrUpdate(Realm realm, Collection<T> collection) { 
     realm.insertOrUpdate(collection); 
    } 

    @Override 
    public T saveOrUpdate(Realm realm, T t) { 
     return realm.copyToRealmOrUpdate(t); 
    } 

    @Override 
    public RealmList<T> saveOrUpdate(Realm realm, RealmList<T> list) { 
     RealmList<T> realmList = new RealmList<>(); 
     for(T t : realm.copyToRealmOrUpdate(list)) { 
      realmList.add(t); 
     } 
     return realmList; 
    } 

    @Override 
    public RealmQuery<T> query(Realm realm) { 
     return realm.where(clazz); 
    } 

    @Override 
    public void deleteEveryObject(Realm realm) { 
     realm.delete(clazz); 
    } 

    @Override 
    public void delete(Realm realm, T t) { 
     t.deleteFromRealm(); 
    } 

    @Override 
    public void deleteAll(Realm realm, RealmResults<T> realmResults) { 
     realmResults.deleteAllFromRealm(); 
    } 

    @Override 
    public long count(Realm realm) { 
     return query().count(); 
    } 
} 

而且

public abstract class StringRealmRepositoryImpl<T extends RealmObject> 
     extends BaseRealmRepositoryImpl<T, String> 
     implements RealmRepository<T, String> { 
    public StringRealmRepositoryImpl(Class<T> clazz) { 
     super(clazz); 
    } 

    @Override 
    public T findOne(Realm realm, String id) { 
     return query(realm).equalTo(getIdFieldName(), id).findFirst(); 
    } 

    @Override 
    public void delete(Realm realm, String id) { 
     delete(realm, findOne(realm, id)); 
    } 
} 

而且

public class TeacherRealm extends RealmObject { 
    @PrimaryKey 
    private String id; 
    //getter, setter, etc. 
} 

而且

public class TeacherRepositoryImpl 
     extends StringRealmRepositoryImpl<TeacherRealm> 
     implements TeacherRepository { 
    public TeacherRepositoryImpl() { 
     super(TeacherRealm.class); 
    } 
} 

和存儲庫是可擴展的。

+1

我不明白TeacherRepositoryImpl中的方法getId和setId,看起來像什麼都不做,你能解釋一下嗎? –

+1

@CarlosAlbertoMurillo有效的問題。我檢查了我使用此設置的項目,因爲我認爲我爲**添加了**,但顯然我似乎無法在任何地方找到它們。我想在一段時間後,我不再需要它們了。所以我把它從答案中刪除了,謝謝你的提示。 – EpicPandaForce

相關問題