Kim Montano
Kim Montano

Reputation: 2285

Check if there any changes to realm object

If the realmObject from a result has a value changed, is there a way to detect it?

Account account = mRealmInstance.where(Account.class).equalTo("isLoggedIn", true).findFirst();

account.setName("New Name");

if(account.hasChanged()){ //Is there a realmMethod for this?

}

Upvotes: 4

Views: 3109

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

I'd assume that this is what you're looking for?

RealmChangeListener<Account> listener = new RealmChangeListener() {
    @Override
    public void onChange(Account account) {
        // changes have been made to Account table
    }
}

Account mAccount;

mAccount = mRealmInstance.where(Account.class).equalTo("isLoggedIn", true).findFirst();
if(mAccount != null) {
    mAccount.addChangeListener(listener);
    //assuming I'm in a transaction here
    mAccount.setName("New Name");
}

...

if(mAccount.isValid()) {
    mAccount.removeAllChangeListeners();
}

Although I do think the RealmChangeListener is activated whenever there's a change to the Account table, and not just when this particular object is modified.

(EDIT: since Realm 3.1+ the realm object listeners are also fine-grained so it is modified only when the selected account is modified)

Upvotes: 5

Related Questions