如何从Realm数据库Android中删除对象?

我想从域中删除所有的消息对象,这些都等于用户标识符

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

在0.88.3及以下,你可以这样做:

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

从0.89(下一版本),这将是deleteAllFromRealm()

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

这必须在realm.beginTransaction(); 和realm.commitTransaction(); 我还在代码示例中列出了几个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(); 

这是我如何使用它

  RealmResults<CartDBItems> rows= realm.where(CartDBItems.class).equalTo("id", id).findAll();; rows.deleteAllFromRealm();