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.