ios - Realm 对象服务器创建数据库和 ROS 但不同步

标签 ios swift swift2 realm realm-mobile-platform

我已经在 AWS (RHAT) 上设置了一个 Realm Object Server,它似乎运行良好。我可以查看仪表板,它会告诉我有多少个连接以及多少个 Realm 在端口 9080 上打开。我的代码在设备上运行良好,更新正在设备 Realm 上发生。但我没有看到 ROS Realm 有任何变化。我可能犯了一些非常基本的错误,但我说不出是什么。

func setupRealm() {
    // Authenticating the User
    let username = "exampleuser"
    let password = "examplepassword"
    SyncUser.logInWithCredentials(SyncCredentials.usernamePassword(username, password: password), authServerURL: NSURL(string: "http://example.com:9080")!, onCompletion:
    { user, error in
        if let user = user {
            // Opening a remote Realm
            let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
            let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
            let realm = try! Realm(configuration: config)
            // Any changes made to this Realm will be synced across all devices!
        } else if let error = error {
            // handle error
        }
    })
}

我从 ViewDidLoad 调用 setupRealm()。

我的开发人员需要能够看到 ROS Realm 的变化,以确保一切正常工作,但那里只有单个用户 Realm ,表结构准确,数据为零。

我检查了服务器上的日志文件,没有发现任何异常。

马特

最佳答案

您需要确保在进行写入时仍然指的是特定的 Realm(及其同步配置)。如果您尝试写入 让 realm = try! Realm() 即使在这次登录之后,您仍然会引用您的默认 Realm,它是不同步的。

如果你打算让你的整个应用程序同步,你可以将特定的 Configuration 设置为你的默认 Realm:

if let user = user {
    // Opening a remote Realm
    let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
    let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL))
    Realm.Configuration.defaultConfiguration = configuration
    // All calls to `Realm()` will now use this configuration
}

否则,您总是可以在需要时生成配置(或者有一个可以为您管理它的全局常量类)。

let realmURL = NSURL(string: "realm://example.com:9080/~/VWF3")!
let configuration = Realm.Configuration()
configuration.syncConfiguration = SyncConfiguration(user: SyncUser.current, realmURL: realmURL))
let realm = try! Realm(configuration: configuration)

关于ios - Realm 对象服务器创建数据库和 ROS 但不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917092/

相关文章:

Xcode找不到GPUImageHistogramEqualizationFilter,但是可以找到其他过滤器

ios - 检查特定的手势识别器

swift - AudioKit:在 Swift 框架中嵌入 AudioKit

iphone - ARC 释放子类 NSObject,但不释放自定义 NSObject

ios - 我想得到没有时间的今天日期

ios - 我对如何在实例被取消初始化时操作实例内部的属性感到困惑?

ios9 - 问题 XCode 8、Swift 2.3 存档版本在 IOS 9.2.1 -> IOS 9.0 上启动时崩溃

swift 2.0 : Type of expression is ambiguous without more context - Dictionary

ios - React Native 可以在 Swift 中生成 UIButton 实例吗?

ios - 仅在 Swift 中创建一个 Setter