备份和恢复 realm.io 数据库

Backup and Restore realm.io database

我需要备份我的默认 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 对象在缓存中,但我无法清除该缓存,因此我无法创建数据库的新实例。

我可以重新加载 RLMRealm 对象吗?

代码如下:

    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)
    }

备份 Realm.io 数据库的最佳方法是什么?

从 Dropbox 备份和恢复 Realm 文件应该可以正常工作,因为数据库只是一个文件。但是,在删除现有领域之前,您必须确保没有对现有领域的任何引用。