无法从Swift中的Realm对象Server获取数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从Swift中的Realm对象Server获取数据相关的知识,希望对你有一定的参考价值。

我想使用Realm Object Server在智能手机之间共享数据,我用Swift创建了这个代码。 (我使用Realm - > 3.0.2 ROS - >最新)

let realmAuthURL = URL(string:"http://[server_address]:9080")!
let realmURL = URL(string:"realm://[server_address]:9080/~/realm")!
let credentials = SyncCredentials.usernamePassword(username: "[username]", password: "[password]")

SyncUser.logIn(with: credentials, server: realmAuthURL) { user, error in
    DispatchQueue.main.async {
        if let user = user {
            Realm.Configuration.defaultConfiguration = Realm.Configuration(
                syncConfiguration: SyncConfiguration(user: user, realmURL: realmURL),
                objectTypes: [PCData.self]
            )
            print("ログインに成功しました")
            self.realm = try! Realm(configuration: Realm.Configuration.defaultConfiguration)
            //print(self.realm.objects(PCData.self))

            if self.realm.isEmpty {
                try! self.realm.write {
                    let data = PCData()
                    data.isOut = false
                    data.pcCode = "***METADATA***"
                    data.rentPCto = ""
                    data.IDinCourse = 999999999999
                    data.belonging = "Master"

                    self.realm.add(data)
                }
            }
        }


    }
}

当我尝试它时,输出“登录成功”,并且服务器具有看起来已被访问的日志

然而,

2017-12-17 22:53:30.880875+0900 PCManage[7021:1784833] Sync: Connection[1]: Connection closed due to error
2017-12-17 22:53:31.673562+0900 PCManage[7021:1784833] Sync: Connection[1]: Connected to endpoint '[Server's IP]:9080' (from '[My IP]:57032')
2017-12-17 22:53:31.684189+0900 PCManage[7021:1784833] Sync: Connection[1]: Writing failed: End of input 

在服务器中无法获取数据等。

proxy: attempting to upgrade client [My IP]:57032 => headers: {"authorization":"Realm-Access-Token version=1 token="eyJhY2Nlc3MiOlsiZG93bmxvYWQiLCJ1cGxvYWQiLCJtYW5hZ2UiXSwiYXBwX2lkIjoiY29tLmFrYmFydC5hcHAuUVJ0ZXN0IiwiZXhwaXJlcyI6MTUxMzUxOTQwOCwiaWQiOiIwMzdmY2I5Yzc0MGE0ZjdhOGVhOTZkZmFhYjUxNmM2ZiIsImlkZW50aXR5IjoiZmI0NmQ1ZjNjYjRiNmE5NWExYWYyNDkzZGIxNjE5YzYiLCJwYXRoIjoiL2ZiNDZkNWYzY2I0YjZhOTVhMWFmMjQ5M2RiMTYxOWM2L3JlYWxtIn0=:Tn4Qa4vzvZ87lnW2htmt6rPCaLL5ASDkpqbcXT2eKskQ3NHJ3ePyMxvJjjyAq5rh8pb2syCM1Dh8LrVk9RilQsHwHRU9HyDOky/psePE4UfXidEJGYoML19R9mo+yjHPG7DeP0ykYltTjONJh+OpQkaelHYIMlKAwFP+hxeNWlVAIpjohkqmyr/de3++8IxcVV6/6wJUESdNpDq2e5GGKfxpSwbfhBagNi0m0aSNuwzoqJ5CFqbyoxsyyCZWx6XJ7ZueTmRvGx22SJcthRzxrQRrWMbMtF8yEHY/AARzycO/A/Zi5spXqAzQD6QIrkl5gGI/FwvnLvhvtPEFychXIg=="","connection":"Upgrade","host":"[]","sec-websocket-key":"K0Tw7zhY3ov9h+VEqHtaPQ==","sec-websocket-protocol":"io.realm.sync.22","sec-websocket-version":"13","upgrade":"websocket"}.}
sync: HTTP Connection[959]: Connection from 127.0.0.1:35414
sync: HTTP Connection[959]: Connection initiates HTTP receipt
sync: HTTP Connection[959]: HTTP request received, url = /realm-sync/%2Ffb46d5f3cb4b6a95a1af2493db1619c6%2Frealm
sync: HTTP Connection[959]: 404 Not Found
sync: HTTP Connection[959]: Connection is closed after HTTP response.

404 Not Found显示我认为服务器端现在很奇怪,但我没有改变初始设置,所以我不知道哪里出错了如何获取数据?

答案

您共享的日志消息似乎表明您正在使用Realm Object Server v1.x. Realm Swift 3.x需要Realm Object Server v2.x.有关安装ROS v2.x的说明,请参阅Realm Object Server documentation

以上是关于无法从Swift中的Realm对象Server获取数据的主要内容,如果未能解决你的问题,请参考以下文章

Realm (Swift):如何在迁移期间获取 MutableSet 数据?

Android Realm Schema不匹配错误

使用 Swift 过滤 Realm 对象

在 Swift 中设置 Realm 的位置

如何在 Swift 中的 Realm.io 中做抽象实体

如何在 Swift 中将 Realm 对象转换为 JSON?