在删除Realm文件之前,如何在Android中备份Realm DB.有什么办法可以还原备份文件? [英] How to backup Realm DB in Android before deleting the Realm file. Is there any way to restore the backup file?
问题描述
我正在处理Android
应用程序,在将新数据复制到Realm之前,我将删除Realm
.我有什么办法可以在删除数据之前备份数据,如果使用realm.copyToRealm()
时出现问题,可以将其还原回来吗?
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 the data before deleting it and restore it back if something is wrong when using realm.copyToRealm()
?
推荐答案
Realm.writeCopyTo
在这种情况下可能会有所帮助.您可以在此处找到文档.
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);
但是,在您的情况下,将Realm文件移至要备份的位置,然后在要还原时将其移回,将更加简单快捷.要获取Realm文件路径,请尝试:
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();
这篇关于在删除Realm文件之前,如何在Android中备份Realm DB.有什么办法可以还原备份文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!