本文介绍了如何在Android中清除Realm中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户按下注销按钮时清除整个数据库,并在另一个用户登录时加载新数据.我尝试了许多解决方案,例如

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.deleteRealm(realmConfiguration);   
    } catch (Exception ex){
        throw ex;
    }

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

但是这两个代码都不起作用.预先感谢.

But neither of the code works.Thanks in advance.

推荐答案

调用,您必须确保所有Realm实例均已关闭,否则将引发异常而不会删除任何内容.通过调用此方法,将删除所有Realm文件,这意味着所有对象&模式不见了.对于所有一般情况而言,捕获所有异常都是一种不好的做法.

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.

或者您可以调用 Realm.delelteAll() 在交易块中.这并不需要关闭所有Realm实例.它只会删除领域中的所有对象,而不会清除架构.同样,不要捕获所有异常.

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.

这篇关于如何在Android中清除Realm中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:28