Lee Jeongmin
Lee Jeongmin

Reputation: 893

android realm : Restoring realm DB from backup file

I'm working on a note app with realm. I'd like to add backup and restore features. So I've been implementing these features as introduced in this page. Here is the user scenario.

  1. When an user click Backup button, back up default.realm to temp.realm
  2. And an user click restore button, replace default.reaml to temp.realm.
  3. Reload data and display with new data(replaced default.realm file).

It works well if I restart my application. But I want to refresh new data immediately. If I did not restart my application, it works weird. So my question is how can i reload restored realm db so that display restored data without application restarting.

Upvotes: 1

Views: 1744

Answers (1)

beeender
beeender

Reputation: 3565

It is very important that before copying the temp.realm to default.realm, all opened Realm instances referring to the old default.realm need to be closed. Since those instances still hold a file descriptor to it, and the newly opened Realm instances after copying will hold a different file descriptor and Realm still think they are them same Realm since the path is exactly the same. This is a situation Realm cannot handle, and some strange things could happen.

You can call Realm.deleteRealm() to delete the old default.realm first. That API will throw if there is any other Realm instance to the realm file which is going to be deleted opened.

Upvotes: 4

Related Questions