How to delete object from Realm Database Android?
In 0.88.3 and below you can do:
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<Message> rows = realm.where(Message.class).equalTo(Message.USER_ID,userId).findAll();
rows.clear();
}
});
From 0.89 (next release) this will be deleteAllFromRealm()
instead.
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<Message> result = realm.where(Message.class).equalTo(Message.USER_ID,userId).findAll();
result.deleteAllFromRealm();
}
});
This must be done between the realm.beginTransaction();
and the realm.commitTransaction();
I also listed in the code example a few args()
.
realm.beginTransaction();
MessageObject messageobj = realm.where(Message.class)
.findFirst() //or
.greaterThan("age", 10) // implicit AND
.beginGroup() //or you can use
.equalTo("name", "Peter")
.or()
.contains("name", "Jo")
.endGroup()
.findAll();
messageobj.deleteFromRealm();
realm.commitTransaction();