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

时间:2015-07-12 15:46:21

标签: java android realm

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

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

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

2 个答案:

答案 0 :(得分:7)

编辑:Realm 0.88.0 has enabled the usage of custom methods,您还可以实现与Realm对象的接口。但我保留了我的存储库模式,这是下面的原始答案。撰写本文时的最新版本为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 :(得分: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();