MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Realm Sync With Swift

I have enabled Realm Sync,
When I tried Realm Sync client Swift in Xcode 12 after adding realm sync framework and pod configuration, the following exception is blocking me to proceed in Future, Promise code.

The code where the problem is

    app.login(credentials: Credentials.anonymous) {(result)  in
            // Remember to dispatch back to the main thread in completion handlers
            // if you want to do anything on the UI.
            DispatchQueue.main.async {
                switch result {
                case .failure(let error):
                    print("Login failed: \(error)")
                case .success(let user):
                    print("Login as \(user) succeeded!")
                    // Continue below
                    self.OnLogin()
                default:
                    self.OnLogin()
                }
            }
        }

and the exception is
Contextual closure type ‘(RLMUser?, Error?) -> Void’ expects 2 arguments, but 1 was used in closure body.

Can any expert help me as soon as possible

You’re using an older version of the Pod/SDK. With 10.0.0 all Realm closures changed from

{ xxxx, error in

to

{ result in

Update your podfile to use this

pod 'RealmSwift', '=10.0.0'

and then

pod update

You may also need to update cocoapods in general with

sudo gem install -n /usr/local/bin cocoapods

Got stuck on this too. Make sure you have cocoa pods version 1.10.0

to check type

pod --version
1 Like

Thanks for the response and Solution,
I have made some changes in the Ream Database by mistake, Since then I could not open the Realm Object in the Client.
The Error message is “The Server has forgotten about this client to resume synchronization”
And the server side Error message is
client file not found { sessionIdent: 1, clientFileIdent: 4 } (ProtocolErrorCode=208)
Please help, Tried re create new project still does not help.

I have tried work around, like creating new Realm app in the Realm (Mongodb Atlas) and try to run the client.
Because of this I need to change the app id in the client.
But this time , the login is failed ( unanimous login ) and the error message is “Malformed JSON”
When I revert back the old app id it is logging in successfully.

You’ll probably need to go into the console, Terminate Sync and re-set it up. Ensure you select Developer Mode tab before doing so.

Also, you may need to completely delete all of the local files - that will cause it to re-sync the data from the server to the client (ensure you have a backup in case there’s local data you need).

Thanks Jason,
The deletion all at server only? (Mongodb atlas , realm app).
Please help me to understand which local files to be deleted if so.

But still I have to cross the new issue I have posted ( Malformed Json while logging)

I think you mean anonymous login. You need to ensure anonymous login is on. In console, go into any app then left column->Authentication then Authentication Providers tab and ensure it’s turned on. Don’t forget to review and deploy changes.

Second thing is there were recent changes to how users authenticate. See Anonymous Authentication - ensure your code matches let anonymousCredentials = Credentials.anonymous

Then as far as local files, if you’re doing iOS you need to reset the simulator. If your doing macOS you need to delete the files in user/library/Application Support/com.your_company.app_name. Again though, deleting those files will reset the client and cause all of the data on the server to re-sync so don’t do it unless you want local data erased.

Thanks a lot Jayson for the clear and detailed clarifications. It helped me lot to understand.

I find the anonymous authentication is enabled, and re sync,
But app.login for anonymous object comes out with nil RLMUser object and never return result, and I dont find and log showing in the server for login failure.
I could not able to make out for nil RLMUser object at client side.
Please help if it has been already encountered by some one resolved it.

That’s a little unclear. Based on your updated code that should look like this

your_app.login(credentials: creds) { result in
    switch result {
    case .failure(let error):
        print(error.localizedDescription)
    case .success(let user):
        print("anon login success")
        print(user.id) //just to show the id
    }
}

does the code in the .failure case run or does the code in the .success run, put printing user.id prints nil?

Yes exactly, I am using the same code, it never goes to switch statement and skips it.
Means, it is not either success nor failure.
And I find in debug, that the RLMUser object is nil and therefore further I cannot use this user object to open the Realm.

One more clue is I find it returns RLMTranslator error in one of the functions before come back to app.login method.

@ALALASUNDARAM_SARAVA you mention ‘it never goes to switch statement and skips it’ Are you waiting for the result to complete? As this is an asynchronous call.

1 Like

Dear Lee_Maguire

Thanks for the response, yes, it is my fault, after correcting the code it is back to the error “Malformed Json”

@ALALASUNDARAM_SARAVA do you have any logs in the MongoDB Realm UI that can help narrow down the issue?

I would like to see the exact code in question and how any vars are assigned.