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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With