不推荐使用SyncConfiguration,SyncUser.configuration()的正确用法是什么?

时间:2018-07-23 22:18:23

标签: swift realm

我已经开始从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!)

但是,这不允许我连接到同步的Realm。使用上面的方法,也无法指定要同步的对象类型。是否有人离开了已弃用的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)

1 个答案:

答案 0 :(得分: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