How to delete object from Realm Database Android?

2019-01-17 19:32发布

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

RealmQuery<Message> rowQuery = realm.where(Message.class).equalTo(Message.USER_ID, userId);
realm.beginTransaction();
//TODO : here I want to remove all messages where userId is equal to "9789273498708475"
realm.commitTransaction();

4条回答
女痞
2楼-- · 2019-01-17 19:47

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();
    }
});
查看更多
唯我独甜
3楼-- · 2019-01-17 19:51

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();
查看更多
祖国的老花朵
4楼-- · 2019-01-17 19:53

this is how i Used it

  RealmResults<CartDBItems> rows= realm.where(CartDBItems.class).equalTo("id", id).findAll();;
         rows.deleteAllFromRealm();
查看更多
男人必须洒脱
5楼-- · 2019-01-17 20:09
 myRealm.beginTransaction();

     RealmResults<Datos> datos = myRealm.where(DatosCliente.class)
                        .equalTo("folio",FOLIO)
                        .findAll();

 datos.deleteAllFromRealm();
查看更多
登录 后发表回答