RealmObjects中的自定义方法…解决方案?

我在我的android应用程序中使用Realm作为数据模块,尽管我遇到了一些关于如何为Realm对象分配自定义方法的问题。 在iOS coredata模块中,您有NSObjects,它们可以保存到内存中,并且在各自的类中也允许延展性。 我可以使用什么样的系统来解决这个问题? 我尝试为我的所有领域对象制作“父类”,但这不起作用。

例如:“TeacherRealm”只包含教师对象的getter和setter,而“TeacherParent”允许用户运行某些算法来查找教师的数据。 在TeacherParent类中,我尝试初始化TeacherRealm,在TeacherRealm中,我为TeacherParent对象提供了一个getter和setter。 低,请注意,我的TeacherRealm对象类不支持自定义TeacherParent对象。

有没有人遇到过这个问题/找到了解决方案呢? 我知道这听起来很混乱,但如果有必要,我可以提供更多信息

编辑: Realm 0.88.0已启用自定义方法 ,您还可以实现与Realm对象的接口。 但我保留了我的存储库模式,这是下面的原始答案。 撰写本文时的最新版本为0.88.1。


我坚信data对象应该只包含数据,逻辑应该是分开的。

 public interface RealmRepository { T findOne(Realm realm, ID id); RealmResults findAll(Realm realm); void insertOrUpdate(Realm realm, T t); void insertOrUpdate(Realm realm, Collection t); T saveOrUpdate(Realm realm, T t); RealmList saveOrUpdate(Realm realm, RealmList tList); RealmQuery query(Realm realm); void delete(Realm realm, ID id); void delete(Realm realm, T t); void deleteAll(Realm realm, RealmResults realmResults); void deleteEveryObject(Realm realm); long count(Realm realm); } 

 public abstract class BaseRealmRepositoryImpl implements RealmRepository { protected Class clazz; public BaseRealmRepositoryImpl(Class clazz) { this.clazz = clazz; } @Override public RealmResults findAll(Realm realm) { return query().findAll(); } @Override public void insertOrUpdate(Realm realm, T t) { realm.insertOrUpdate(t); } @Override public void insertOrUpdate(Realm realm, Collection collection) { realm.insertOrUpdate(collection); } @Override public T saveOrUpdate(Realm realm, T t) { return realm.copyToRealmOrUpdate(t); } @Override public RealmList saveOrUpdate(Realm realm, RealmList list) { RealmList realmList = new RealmList<>(); for(T t : realm.copyToRealmOrUpdate(list)) { realmList.add(t); } return realmList; } @Override public RealmQuery 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 realmResults) { realmResults.deleteAllFromRealm(); } @Override public long count(Realm realm) { return query().count(); } } 

 public abstract class StringRealmRepositoryImpl extends BaseRealmRepositoryImpl implements RealmRepository { public StringRealmRepositoryImpl(Class 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 implements TeacherRepository { public TeacherRepositoryImpl() { super(TeacherRealm.class); } } 

并且存储库是可扩展的。

我正在研究类似的问题。 我的方法是使用静态’helper’方法来实现所有额外的逻辑。 在方法调用中传递我的对象类型的实例。

我有同样的情况在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();