如何使用领域数据库的副本

时间:2017-11-21 22:47:17

标签: swift database realm realm-mobile-platform

我正在使用XCODE,我从default.realm到realm数据库的副本 user_xxx.realm。我需要在同一设备中管理多个用户。所以我添加了一个后缀。新的数据库似乎是空的,它在模拟器上工作但在真实设备上没有任何想法?

        let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let realmNew = documentDirectory.appendingPathComponent("database_" + String(id) + ".realm")
    let realmDefault = documentDirectory.appendingPathComponent("default.realm")

                do {
                    //try FileManager.default.moveItem(at: realmDefault, to: realmNew)
                    try fileManager.copyItem(atPath: realmDefault.path, toPath: realmNew.path)
                    result = "STEP 3"
                    migration = true  
                }
                catch let error as NSError {
                    result = "STEP 4"
                    print("Ooops! Something went wrong: \(error)")
                }

    config.fileURL = realmNew
    Realm.Configuration.defaultConfiguration = config

0 个答案:

没有答案