【问题标题】:SyncConfiguration deprecated, what is the proper use of SyncUser.configuration()?SyncConfiguration 已弃用,SyncUser.configuration() 的正确用途是什么?
【发布时间】:2018-07-23 22:18:22
【问题描述】:

我已经开始收到来自 Realm 的以下警告:

'init(user:realmURL:enableSSLValidation:isPartial:urlPrefix:)' 已弃用:改用 SyncUser.configuration()

我明白警告的意思,但我无法让SyncUser.configuration() 正常工作。这是我当前(已弃用)的实现:

if let _ = SyncUser.current {
    // already logged in
    let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Settings.Credentials.REALM_URL!)
    let config = Realm.Configuration(syncConfiguration: syncConfig, objectTypes: [ConfigItem.self, Intersection.self])

    self.database = try! Realm(configuration: config)
}
else {
    let creds = SyncCredentials.usernamePassword(username: Settings.Credentials.REALM_LOGIN, password: Settings.Credentials.REALM_KEY)

    SyncUser.logIn(with: creds, server: Settings.Credentials.AUTH_URL!, onCompletion: { [weak self](user, error) in
        if let _ = user {
            let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Settings.Credentials.REALM_URL!)
            let config = Realm.Configuration(syncConfiguration: syncConfig, objectTypes: [ConfigItem.self, Intersection.self])

            self?.database = try! Realm(configuration: config)
        } else if let error = error {
            fatalError(error.localizedDescription)
        }
    })
}

let syncConfig = ...... 这两行是我收到警告的地方。我可以让应用程序正确编译,像这样替换两个配置行:

let config = SyncUser.current!.configuration(realmURL: Settings.Credentials.REALM_URL!)

但是,这不允许我连接到我的同步领域。使用上述方法,也无法指定要同步的对象类型。有没有人从已弃用的SyncConfiguration 配置同步领域的方式迁移?它似乎应该接近我正在尝试的,但由于某种原因它没有连接。

编辑

如果我使用默认的SyncUser.configuration(),它至少会给我它连接到端点的消息,但数据不同步。这是使用默认同步配置的config

let config = SyncUser.current!.configuration()

如果我重新添加 realmURL 参数,它不再给我连接到端点的消息。此外,不推荐使用的方法仍然是 https://realm.io/docs/swift/latest/#realms 文档中显示的内容:

// Create the configuration
let syncServerURL = URL(string: "realm://localhost:9080/~/userRealm")!
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: syncServerURL))

// Open the remote Realm
let realm = try! Realm(configuration: config)

【问题讨论】:

    标签: swift realm


    【解决方案1】:
    let config = SyncUser.current!.configuration(realmURL: url, fullSynchronization: false, enableSSLValidation: true, urlPrefix: nil)
    

    我从他们的客户支持处获得了这个链接:https://realm.io/docs/swift/3.7.5/api/Extensions/SyncUser.html#/s:So11RLMSyncUserC10RealmSwiftE13configurationAC0C0C13ConfigurationV10Foundation3URLVSg05realmH0_Sb19fullSynchronizationSb19enableSSLValidationSSSg9urlPrefixtF

    【讨论】:

    • 这与SyncUser.current!.configuration(realmURL: url) 相同,因为其余的都是默认值...您是否尝试过修改现有的同步领域以使用此配置?
    • 对。我没有看到您在编辑部分 +1 中回答了您自己的问题
    • @dubstylee 我没有看到 Edit 回答了这个问题;它指出折旧的错误消息已解决,但数据未同步。您能否用更多代码更新答案以显示使用 SyncUser.configuration() 实际连接到 Realm 的正确方法?否则它只是一个链接唯一的答案。
    • @Jay 正确,编辑没有回答问题。我仍然无法让它与 SyncUser.configuration() 一起使用。我现在仍在使用已弃用的代码。
    • 几个月来我一直在网上寻找答案。除了已弃用的代码之外,似乎没有真正的方法可以使用任何代码。
    猜你喜欢
    • 2021-10-17
    • 2016-07-04
    • 2017-06-12
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2015-05-11
    相关资源
    最近更新 更多