3

I need to backup my default realm.io database and restore it from Dropbox. I'm using the Dropbox-iOS-SDK to upload and download it. When I tried to replace the current database file (Ex. default.realm) with the backup, the RLMRealm object was on the cache and I'm not able to clear that cache so I can't create a new instance of the Database.

Can I reload the RLMRealm object?

Here is the code:

    func reloadDB(fromPath: String) {
        let defaultParentPath = RLMRealm.defaultRealmPath().stringByDeletingLastPathComponent
        let dbPath = defaultParentPath.stringByAppendingPathComponent("db.realm")
        let dbLockPath = defaultParentPath.stringByAppendingPathComponent("db.realm.lock")

        NSFileManager.defaultManager().removeItemAtPath(dbPath, error: nil)
        NSFileManager.defaultManager().removeItemAtPath(dbLockPath, error: nil)

        NSFileManager.defaultManager().copyItemAtPath(fromPath, toPath: dbPath, error: nil)

        self.db = RLMRealm(path: dbPath, readOnly: false, error: nil)

        NSFileManager.defaultManager().removeItemAtPath(fromPath, error: nil)
    }

What is the best way to backup a Realm.io database?

Nicopuri
  • 901
  • 2
  • 11
  • 17

1 Answers1

1

Backing up and restoring a Realm file from dropbox should work just fine, since the database is just a file. However, you must make sure you don't have any references to the existing realm before deleting it.

segiddins
  • 4,110
  • 1
  • 17
  • 22