Realm at path ... already opened with different sync user

In Realm Swift, whenever I log out a user and log back in within the same session (not quitting app in between), I get the error in title.

A few print statements indeed show me that the folder Documents/mongodb-realm/[app-id]/[user-id] is not deleted after logging out and still contains the realm files. I would expect that this folder gets deleted when the user logs out.

  1. Is it normal that this folder is not deleted?
  2. How to avoid this error?

Why would you expect a users data would get deleted without you (or them) implicitly doing that?

Remember, Realm is an offline first (local) database so the users data is stored locally on the device and then sync’d to the server at a later time. If their data was deleted every time they logged out, that would mean it would have to re-sync when logging in, causing delays and … that would be online first database

…and really run up your bill!

You would need to code that delete for it to happen - I am not sure of your use case but that sounds like an expensive, and time consuming operation.

As far as the error goes - it sounds like your not fully logging out the user.

Can you share the specific (and brief) code you’re using to log the user out?

1 Like

I am experiencing the same issue. How to fully log out a user?

This is how we log out a logged in user. Never had an issue with it

func handleSignOut() {
    gOurApp.currentUser?.logOut { (error) in
        guard error == nil else {
            print("Logout failed: \(error!.localizedDescription)");
        print("Logged out!");

and we have a global app var that returns the app

let gOurApp = App(id: Constants.REALM_APP_ID)

1 Like

It seems App doesn’t have a logOut method. Do you mean the currentUser? I am using almost the same code as you shared the only difference is I am using it on a user object.

My apologies. Yes it is app.currentUser.logout - I just typod in my answer.

I’m facing the same issue, using the same logout method as above. On re-authenticating with a new user realm crashes.

The app error
Realm at path ‘/var/mobile/Containers/Data/Application/DEVIDEID/Documents/default.realm’ already opened with different sync user.

The mongodb atlas app services error
User mismatch for client file identifier (IDENT) (ProtocolErrorCode=223)

I guess a simple solution would be to add userid to the realm file. I have an open case with support on this

1 Like

Hi @Jimmi_Andersen,

As explained above, the standard path for a realm does include the user id:

If you’re overriding the local path to a fixed default.realm, you’re skipping the logic that would isolate the different users, and there’ll be conflicts when you log one out and another in. Additionally, please note that logOut doesn’t close the opened realm(s) immediately, to be safe you should explicitly nil all the references to each realm (including observers, cursors, …) and leave some time for the different threads to close properly.

Thanks for quick reply and the info, let me try to nil the reference to realm.

FYI the path on iOS device does not include /[user-id]/, and what I was referring to was the mongodb auth user id (not some local userid).

It certainly does include both the app-id and the user-id that’s obtained by authenticating in with the Realm SDK, if we’re talking about Swift (that was the assumption in this thread).

If this doesn’t match your experience, can you please specify which SDK are you using, and the code snippet you use to open a realm, particularly how you set the SyncConfiguration?