如何在Android中清除Realm中的数据库 [英] How to Clear Database in Realm in Android

查看:470
本文介绍了如何在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.

推荐答案

调用

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中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆