How to Clear Database in Realm in Android

后端 未结 2 1927
傲寒
傲寒 2021-02-18 16:04

I want to clear whole database when a user press logout button and loads a new data when another user login.I tried many solutions like

try {
        Realm.delet         


        
相关标签:
2条回答
  • 2021-02-18 16:22

    If you are sure there are not any other databases you want to save, you can delete all the other data also. you can follow this answer Clear Application's Data Programmatically

    0 讨论(0)
  • 2021-02-18 16:25

    When you call Realm.deleteRealm(), you have to make sure all the Realm instances are closed, otherwise an exception will be thrown without deleting anything. By calling this method, all Realm files are deleted, which means all objects & schemas are gone. Catching all exceptions is a bad practise for any general cases.

    Or you can call Realm.delelteAll() in a transaction block. This doesn't require all Realm instances closed. It will just delete all the objects in the Realm without clearing the schemas. And again, don't catch all exceptions.

    0 讨论(0)
提交回复
热议问题