RealmMismatchedConfigException after LogOut and LogIn

You got [Realms.Exception.RealmMismatchedConfigException: Realm at path ‘<path_to_realm>’ already opened with different sync user.] after login with new config instance for same user.

var app = App.Create("appId");

await app.LogInAsync(Credentials.EmailPassword("email", "password"));
var config = new FlexibleSyncConfiguration(app.CurrentUser);
var realm = await Realm.GetInstanceAsync(config);

await app.CurrentUser.LogOutAsync();

// Repeat LogIn code for simplicity
await app.LogInAsync(Credentials.EmailPassword("email", "password"));
config = new FlexibleSyncConfiguration(app.CurrentUser);
realm = await Realm.GetInstanceAsync(config);

// exception thrown !!!

How to proper LogIn after LogOut on the same app instance?
It can’t be that we need to keep every user config instance in memory?!

You’ll need to call realm.Dispose() before you reopen the Realm with the newly logged in user. This is probably something we can/should do automatically, but we don’t today.

Indeed, calling Dispose or putting using statement on local realm helps.

Is there some global method to get know of any opened realms in the more complex app scenario?

We don’t offer something like that out of the box, but you can definitely build it yourself - e.g. by having a RealmService that you use to open Realm instances, which also tracks the ones it has vendored out and offers the option to forcefully dispose of all of them upon user logout. That being said, we’ll investigate whether it makes sense to do that automatically. The risk there is that by closing the Realm file, we could break stuff in your app if it wasn’t designed to handle the database getting closed from underneath it.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.