境界:'Cannot set a default configuration after using per-path configuration methods.' Swift 2

Realm: 'Cannot set a default configuration after using per-path configuration methods.' Swift 2

所以我试图为每个登录的用户设置一个默认领域,根据领域文档,这可以通过在文档后附加一个 userId 并在那里创建一个新领域并将其设置为每个用户的默认领域来完成用户。

我有一个函数,每次用户登录时都会执行下面的代码,以便提供该用户的特定数据。

我收到的错误是

*** Terminating app due to uncaught exception 'RLMException', reason: 'Cannot set a default configuration after using per-path configuration methods.'

但是,根据此处的文档,这似乎是正确的方法:https://realm.io/docs/swift/latest/#realm-configuration

这是 Realm 文档代码:

func setDefaultRealmForUser(username: String) {
var config = Realm.Configuration()

// Use the default directory, but replace the filename with the username
 config.path = config.path.stringByDeletingLastPathComponent()
                       .stringByAppendingPathComponent(username)
                       .stringByAppendingPathExtension("realm")

  // Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
 }

这是我的代码(在 swift 2 中删除了 stringBy... 方法,所以我将其更改为使用 NSURL。但是,Realm 仍然将路径读取为字符串)。

    var config = Realm.Configuration()
    let realm = try! Realm()
    // Use the default directory, but replace the filename with the username
    var url = NSURL()
    url = NSURL(fileURLWithPath: config.path!)
    if let url2 = url.URLByDeletingLastPathComponent {
        let url3 = url2.URLByAppendingPathComponent(userId)
        let url4 = url3.URLByAppendingPathExtension("realm")
        config.path = String(url4)
                // Set this as the configuration used for the default Realm
        if let newPath = config.path {
         Realm.Configuration.defaultConfiguration.path = newPath  //fails here

             print("Successfully changed default realm to \(config.path)")
            return
        }
        print("\n\n\n-------ERRORmid setting default realm to \(config.path)----\n\n\n")
        return
    }
    print("\n\n\n-------ERRORlast setting default realm to \(config.path)----\n\n\n")
    return

我都试过了:

   Realm.Configuration.defaultConfiguration.path = newPath

    Realm.Configuration.defaultConfiguration = config

非常感谢您的帮助!

看起来您正在调用以下已弃用的函数之一:

setSchemaVersion(_:atPath:migrationBlock:)
// or
Realm.setEncryptionKey(_:forPath:)

以及您的 Realm.Configuration 用法。这些方法与 Realm.Configuration 不兼容,因此您应该完全移至 Realm.Configuration 以避免这些问题。

这些已弃用的方法将在 Realm Objective-C 和 Realm Swift.

的下一个主要版本中完全删除