pradeep bishnoi pradeep bishnoi - 2 months ago 19
Android Question

How to delete object from Realm Database Android?

I want remove all message object from relam those are equal to userid

RealmQuery rowQuery =realm.where(Message.class).equalTo(Message.USER_ID,userId);

realm.beginTransaction();

//TODO : here i want remove all message where userId is equeal "9789273498708475"

realm.commitTransaction();

Answer

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();
    }
});
Comments