【问题标题】:Custom methods in RealmObjects... solution?RealmObjects 中的自定义方法...解决方案?
【发布时间】:2015-07-12 15:46:21
【问题描述】:

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

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

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

【问题讨论】:

  • 为什么不想将模型与对模型执行操作的类分开? :困惑:
  • TeacherRealm 类中的静态方法怎么样:public static RealmResults<TeacherRealm> findGoodTeachers(Realm realm, int teacherAge)

标签: java android realm


【解决方案1】:

编辑: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);
    }
}

并且存储库是可扩展的。

【讨论】:

  • TeacherRepositoryImpl中getId和setId方法看不懂,好像什么都不做,能解释一下吗?
  • @CarlosAlbertoMurillo 有效问题。我检查了我使用此设置的项目,因为我认为我添加它是出于某种原因,但显然我似乎无法在任何地方找到它们。我想在一段时间后,我不再需要它们了。所以我把它从答案中删除了,谢谢你的注意。
【解决方案2】:

我正在处理一个类似的问题。我的方法是使用静态“帮助”方法来实现所有附加逻辑。在方法调用中传递我的对象类型的实例。

【讨论】:

  • 是的,这就是我最终所做的,而且效果很好
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2010-12-07
    • 1970-01-01
    • 2011-01-13
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多