Pre save and post read actions

时间:2016-07-11 20:26:08

标签: android realm

I am using realm.io in Android. I just started using realm so my question is bit basic. I want to know

  • How to do pre-save action on realm object ? Like I want to run some calculation on the in coming data before saving them
  • How to do post-read operation before handing over the object?

Thanks

1 个答案:

答案 0 :(得分:0)

这实际上取决于你想要如何实现它。

您可以将函数包装为

public static void setValue(Something something, int value) {
    value = value + 42;
    something.setValue(value);
}

realm.beginTransaction();
setValue(something, 42);
realm.commitTransaction();

或者您可以使用setter来包装逻辑,如:

public class Something extends RealmObject {
    private int value;
    public void setValue(int value) {
        value = value + 42;
        this.value = value;
    }
}

吸气剂类似。