How to backup Realm DB in Android before deleting the Realm file. Is there any way to restore the backup file?

前端 未结 2 646
我寻月下人不归
我寻月下人不归 2020-12-16 22:54

I am working on an Android application where I will be deleting Realm before copying the new data to Realm. Is there any way I can take backup of t

2条回答
  •  时光说笑
    2020-12-16 23:15

    Realm.writeCopyTo might be helpful for this case. You can find doc here.

    //Backup
    Realm orgRealm = Realm.getInstance(orgConfig);
    orgRealm.writeCopyTo(pathToBackup);
    orgRealm.close();
    //Restore
    Realm.deleteRealm(orgConfig);
    Realm backupRealm = Realm.getInstance(backupConfig);
    backupRealm.writeCopyTo(pathToRestore);
    backupRealm.close();
    orgRealm = Realm.getInstance(orgConfig);
    

    But in your case, it would be much simpler and faster to just move your Realm file to a place to backup, and move it back when you want to restore it. To get the Realm file path, try:

    realm.getPath();
    

提交回复
热议问题