Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate all tables in realm android

Tags:

android

realm

Hi im trying to truncateall tables in android when a user logs out. im using realms default path only.

realm = Realm.getInstance(getApplicationContext());

public void clearDB() {
    Realm.deleteRealmFile(instance);
}
like image 482
Dinu Avatar asked Jan 22 '15 09:01

Dinu


People also ask

How to delete table in realm android?

Use realm. delete(Foo. class) instead as clear() is deprecated.

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.

Is Realm mobile database free?

Yes definitely you can use Realm for free to save unlimited data locally. Realm is only charging for cloud storage .


2 Answers

Update

Use realm.delete(Foo.class) instead as clear() is deprecated. From 0.91.0 all @Deprecated methods will be removed.


Christian from Realm here. That approach will work as long as you have closed all open Realm instances. Another approach is clearing the tables you want like this:

realm = Realm.getInstance(getApplicationContext());

public void clearDB() {
  realm.executeTransaction(new Realm.Transaction() {
     @Override
     public void execute(Realm realm) {
        realm.clear(Foo.class);
        realm.clear(Bar.class);
     }
  });
}

You can read more here: http://realm.io/docs/java/0.77.0/api/io/realm/Realm.html#clear(java.lang.Class)

like image 73
Christian Melchior Avatar answered Oct 13 '22 19:10

Christian Melchior


use realm.delete(Myclass.class);

realm = Realm.getDefaultInstance();

    realm.beginTransaction();
    realm.delete(SuggestedAppDto.class);
    realm.delete(WifiSpotsDto.class);
    realm.commitTransaction();
    realm.close();

this worked for me.

like image 26
Sopnil.Shinde Avatar answered Oct 13 '22 20:10

Sopnil.Shinde