简体   繁体   中英

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?

}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM