Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); 
like image 786
Pradeep Bishnoi Avatar asked Apr 20 '16 07:04

Pradeep Bishnoi


People also ask

How do you delete an object from a realm?

Select a realm and click the Edit button. The Edit Realm wizard displays. Click Next to move to the Realm Objects page where you can click Remove to delete objects from the realm. Click Done.

How do I delete all data from a realm?

The right way of deleting your entire Realm (schema) is to use : Realm realm = Realm. getDefaultInstance(); realm. beginTransaction(); // delete all realm objects realm.

How do I clear realm database react native?

To delete all objects from the realm, call Realm. deleteAll() inside of a write transaction. This clears the realm of all object instances but does not affect the realm's schema.

Is Realm mobile database free?

Realm is an open-source database management system that is used to develop mobile applications. And the best part? It can be used for free. The Realm supports Android, iOS, Xamarian, and other Native programming languages.


1 Answers

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();     } }); 
like image 55
Christian Melchior Avatar answered Oct 02 '22 07:10

Christian Melchior