无法使用 mongodb 同步打开领域 [英] can't open Realm using mongodb sync
问题描述
我无法打开 Realm.我的设置有问题.这是应用程序中的代码:
I can't open a Realm. There is something wrong with my set up. This is the code in the app:
var configuration = user.configuration(partitionValue: "user=\(user.id)")
configuration.objectTypes = [User.self]
Realm.asyncOpen(configuration: configuration) { [weak self](userRealm, error) in
self!.setLoading(false);
guard error == nil else {
fatalError("Failed to open realm: \(error!)"). ///// here was my error before the edit (I changed the Permission Settings in Sync)
}
错误代码:致命错误:无法打开领域:错误域=io.realm.unknown Code=89"操作已取消"UserInfo={Category=realm.basic_system, NSLocalizedDescription=操作已取消,错误代码=89"
Error code: "Fatal error: Failed to open realm: Error Domain=io.realm.unknown Code=89 "Operation canceled" UserInfo={Category=realm.basic_system, NSLocalizedDescription=Operation canceled, Error Code=89"
Mongodb Sync 配置截图
Screenshot of Mongodb Sync configuration
将 Sync 中的权限设置替换为 Task Tracker 应用程序中提到的设置并使其连接:
Replaced the Permission settings in Sync with the ones mentioned in the Task Tracker app and that got it connected:
根据 Jay 的建议,配置设置已更改为以下内容.
the configuration settings were changed to the below as per the suggestions from Jay.
var configuration = user.configuration(partitionValue: "\(currentUser.id!)")
这是我对用户集合的 Scheme 定义:
Here is my Scheme definition for the User Collection:
{
"properties": {
"_id": {
"bsonType": "string"
},
"_partition": {
"bsonType": "string"
},
"name": {
"bsonType": "string"
}
},
"required": [
"_id",
"_partition",
"name"
],
"title": "User"
}
这是我在 Xcode 中的 User 类:
This is my User class in Xcode:
class User: Object {
@objc dynamic var _id: String = ""
@objc dynamic var _partition: String = ""
@objc dynamic var name: String = ""
override static func primaryKey() -> String? {
return "_id"
}
}
我通过更新同步权限来建立连接,因此应用程序不会再崩溃.但是,现在我收到此消息:
I got this going to establish a connection by updating the Sync Permissions, so the app does not crash any more. However, now I am getting this message:
Connection to daemon was invalidated
Signup successful!
Log in as user: y
Login succeeded!
2020-10-25...Sync: Connection[1]: Session[1]: client_reset_config = false, Realm exists = false, async open = false, client reset = falseSync: Connection[1]: Session[1]: client_reset_config = false, Realm exists = false, async open = false, client reset = false
2020-10-25...Sync: Connection[1]: Connected to endpoint '13.54.209.90:443' (from '.....:52139')
2020-10-25...Sync: Connection[1]: Reading failed: End of input
2020-10-25...Sync: Connection[1]: Connection closed due to error```
我不明白错误("无法打开领域:(错误!))"在我上面的代码中没有触发,但是在日志中它说该领域不存在!那么,这里发生了什么?
I don't understand that the error "("Failed to open realm: (error!)")" in my above code is not triggered, but then in the log it says that the realm does not exist! So, what's going on here?
这是mongodb中的用户表,所以我成功创建了一些用户.
EDIT : this is the user table in mongodb, so I created some Users successfully.
这是来自 mongodb 的日志
EDIT : This is the log from mongodb
我们可以看到用户 ID 和请求 ID 不一样!我猜这两个ID应该是同一个字符串才能认证吧??
As we can see the User Id and the Request ID are not the same! I guess that the two IDs should be the same string in order to be authenticated, right??
我正在关注 Swift 的 mongodb 网页中的 Task Tracker 应用教程,以将用户登录名添加到我的应用中.我在这里错过了什么?
I am following the Task Tracker app tutorial from the mongodb webpage for Swift to add the user login to my app. What am I missing here?
推荐答案
完全猜测,您的配置字符串不正确
As a complete guess, your config string is not correct
var configuration = user.configuration(partitionValue: "user=\(user.id)")
因为分区值解析为
partitionValue: user=Optional("5f1b586f757611faec257d88")
试试这个
guard let user = your_app.currentUser() else {
print("no user")
return
}
guard let userId = user.id else {
print("no user")
return
}
var configuration = user.configuration(partitionValue: "user=\(userId)")
更重要的是,您尝试使用的分区值是这个字符串
More to the point though, the partition value you're attempting to use is this string
user=5f1b586f757611faec257d88
我认为你真正想要的是使用用户 ID
and I think what you really want is use the user id
5f1b586f757611faec257d88
这就是我要开始的地方.如果您试图利用 Realm 规则,那么像 _partitionKey: "team_id=1234"
这样的东西会起作用,但这超出了原始问题的范围(并增加了另一层复杂性 - 明白了先工作,再探索规则).
That's where I would start. If you're trying to leverage Realm rules, then something like _partitionKey: "team_id=1234"
would work but that goes beyond the scope of the original question (and adds another layer of complexity - get it working first, then explore the rules).
这篇关于无法使用 mongodb 同步打开领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!