Android Realm Default Database

How to clear the default database in android? I tried the following code but cannot resolve the deleteRealmFile method.

Method 1:

try { Realm.deleteRealmFile(context); //Realm file has been deleted. } catch (Exception ex){ ex.printStackTrace(); //No Realm file to remove. } 

I tried to uninstall using the configuration.

Method 2:

 try { Realm.deleteRealm(realm.getConfiguration()); //Realm file has been deleted. } catch (Exception ex){ ex.printStackTrace(); //No Realm file to remove. } 

but gives an error:

 java.lang.IllegalStateException: It not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file. 
+5
source share
2 answers

As described above, you need to close all Realm instances that belong to a specific file in the area.

That means if you called

Realm realm = Realm.getInstance(config);

You need to close the area before deleting it.

realm.close();

And Realm instances are based on a reference counter, so make sure that each getInstance has an associated close .

This is very important, otherwise a memory leak may occur. For some examples, see https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances .

+2
source

To Realm.deleteRealm(realm.getConfiguration()); just add realm.close() as below and it works like a charm

 try { realm.close() Realm.deleteRealm(realm.getConfiguration()); //Realm file has been deleted. } catch (Exception ex){ ex.printStackTrace(); //No Realm file to remove. } 
+6
source

Source: https://habr.com/ru/post/1239110/


All Articles