can't open Realm using mongodb sync

Viewed 1258

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)
                }

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"

Screenshot of Mongodb Sync configuration

Screenshot of Mongodb Sync configuration

EDIT: Replaced the Permission settings in Sync with the ones mentioned in the Task Tracker app and that got it connected:

enter image description here

EDIT: the configuration settings were changed to the below as per the suggestions from Jay.

var configuration = user.configuration(partitionValue: "\(currentUser.id!)")

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"
}

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"
}
}

EDIT: 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?

EDIT : this is the user table in mongodb, so I created some Users successfully.

enter image description here

EDIT : This is the log from mongodb

enter image description here

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??

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?

1 Answers

As a complete guess, your config string is not correct

var configuration = user.configuration(partitionValue: "user=\(user.id)")

As that the partition value resolves to

partitionValue: user=Optional("5f1b586f757611faec257d88")

Try this

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

and I think what you really want is use the user id

5f1b586f757611faec257d88

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).

Related