RealmObject and two way data binding


#1

I’ve been working on an android app with RealmObjects that sort of use what is outlined in this article: https://medium.com/@Zhuinden/realm-1-2-0-android-data-binding-1dc06822287f

Basically, I’m not using the factory, just putting in the methods that allow it to implement the Observable interface required for data binding. I want the objects to be unmanaged when they are connected via data binding to the fields on the activity so if the user decides to cancel the changes they made in the activity nothing is changed in realm.

For starting an activity here’s what happens:

  • If it is creating a new record, just instantiate a new object of that class
  • If it is editing an existing record, load it from realm and then do a realm.copyFromRealm(realmObj); so it is unmanaged

For ending an activity here’s what happens:

  • If the user cancels the editing then just let the natural garbage collection happen to the object when the activity is destroyed.
  • If the user saves the editing then within a realm.executeTransactionAsync() call r.insertOrUpdate(unmanagedObj); (where r is the realm object passed into the lambda) the object is then allowed to be garbage collected when the activity is destroyed.
  • In both cases, realm.close() is called when the activity is destroyed.

So I’ve got two questions:

  • Is that a reasonable way to do it or are there some memory issues that I’m creating?
  • Would it be bad to reuse the object in another activity after it was passed into r.insertOrUpdate(unmanagedObj); or is that going to create issues?