简体   繁体   中英

How to delete object from Realm Database Android?

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

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

this is how i Used it

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

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

 datos.deleteAllFromRealm();

For below v10 realm android users. Given below will work fine

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Note> result = realm.where(Notes.class).equalTo(Note.NOTE_ID,noteID).findAll();
        result.deleteAllFromRealm();
    }
});

However, it will crash for v10 and above

Running transactions on the UI thread has been disabled.

Your app will not crash if you run execute transactions from the UI thread as long as you are running Realm prior to v10. For the upcoming v10 release we are introducing two new settings in RealmConfiguration.Builder , namely allowWritesOnUiThread and allowQueriesOnUiThread , which will allow users to control whether it is allowed to run transactions and/or queries from the UI thread.

RealmConfiguration config = new RealmConfiguration.Builder()
  .allowWritesOnUiThread(true)
  .build()

And it will work like it always has. So you can decide when/if you want to opt into the new behavior.

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