1

I'm building a little Android app and I find the ActiveRecord pattern pretty handy. But since ActiveAndroid development is quite dead, I turned to Realm instead.

I'm surprised nothing is said about this pattern, is it safe to write something like this ?

public void delete(){
    Realm realm=Realm.getDefaultInstance();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            MyClass.this.deleteFromRealm();
        }
    });
    realm.close();
}
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • Yes, that will work fine, but remember to close the instance before exiting the method, otherwise you risk leaking instances. – Christian Melchior Apr 03 '17 at 06:52
  • Realm by default works using `active record pattern`, which is why you're literally just delegating the `delete()` call to the object itself. :p (please close your Realm instances) – EpicPandaForce Apr 03 '17 at 07:06
  • Thanks ! But then, why doesn't Realm provide methods like RealmObject.save() and RealmObject.delete() ? This would avoid opening a transaction each time... – Dan Chaltiel Apr 03 '17 at 15:19
  • More, I think that you cannot make a child class of RealmObject with custom methods that my entities would extend, isnt it ? Should I then copy this code to every class ? – Dan Chaltiel Apr 03 '17 at 15:21

0 Answers0