Skip to content

[RLM_ERR_MISMATCHED_CONFIG]: Realm at path '.../files/default.realm' already opened with a different schema mode. #1876

@msmartins86

Description

@msmartins86

I’m opening this ticket hoping you can spare me the cost of hiring a witch to solve this sorcery for me.

In the Android app I’m developing, I had issues with manual migrations in the previous version, and to prevent that from happening again, I implemented the following solution in the current version:

  1. Copy the default.realm file to a new file called sandbox.realm
  2. Try to open the sandbox Realm and run the required data migration
  3. Close the Realm and delete the sandbox file
  4. Open the default Realm normally
  5. If an error occurs during the sandbox migration I open the fallback Realm, which has the same schema as the default but uses the deleteRealmIfMigrationNeeded configuration instead of manual migration

However, I’m getting the following error in Crashlytics:
[RLM_ERR_MISMATCHED_CONFIG]: Realm at path '.../files/default.realm' already opened with a different schema mode.

It only appears for a very small number of users, and I haven’t been able to reproduce the issue at all.

Does anyone have any idea what might be causing this?

The code (I removed insignificant code not related to realm operations):

private val realmConfigurationBuilder = RealmConfiguration.Builder(
    setOf(
        Model1::class,
        Model2::class
    ),
)

@Provides
@Named(REALM_DEFAULT)
fun provideRealmConfiguration(): RealmConfiguration = realmConfigurationBuilder
    .name(REALM_DEFAULT_NAME)
    .schemaVersion(REALM_SCHEMA_VERSION)
    .migration(RealmMigration())
    .build()

@Provides
@Named(REALM_SANDBOX)
fun provideRealmConfigurationSandbox(): RealmConfiguration = realmConfigurationBuilder
    .name(REALM_SANDBOX_NAME)
    .schemaVersion(REALM_SCHEMA_VERSION)
    .migration(RealmMigration())
    .build()

@Provides
@Named(REALM_FALLBACK)
fun provideRealmConfigurationFallback(): RealmConfiguration = realmConfigurationBuilder
    .name(REALM_DEFAULT_NAME)
    .schemaVersion(REALM_SCHEMA_VERSION)
    .deleteRealmIfMigrationNeeded()
    .build()

@Provides
@Singleton
@Named(REALM)
fun provideUserPrefsRealmInstance(
    @Named(REALM_DEFAULT)
    realmDefaultConfiguration: RealmConfiguration,
    @Named(REALM_SANDBOX)
    realmSandboxConfiguration: RealmConfiguration,
    @Named(REALM_FALLBACK)
    realmFallbackConfiguration: RealmConfiguration,
    @ApplicationContext context: Context,
): Realm = runBlocking(Dispatchers.Default) {
    mutexUserPrefsRealm.withLock {
        val realm = try {
            userPrefsRealmSandbox(
                context = context,
                configuration = realmSandboxConfiguration,
            )

            Realm.open(configuration = realmDefaultConfiguration)
        } catch (ex: IllegalStateException) {
            Realm.open(configuration = realmFallbackConfiguration)
        } catch (ex: IllegalArgumentException) {
            throw ex
        }
        realm
    }
}

private fun userPrefsRealmSandbox(context: Context, configuration: RealmConfiguration) {
        val originalFile = File(context.filesDir, REALM_DEFAULT_NAME)
        val sandboxFile = File(context.filesDir, REALM_SANDBOX_NAME)

        if (originalFile.exists()) originalFile.copyTo(target = sandboxFile, overwrite = true)

        val sandboxRealm = Realm.open(configuration = configuration)

        sandboxRealm.close()
        Handler(Looper.getMainLooper()).postDelayed({
                Realm.deleteRealm(configuration = configuration), 1000
        )
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions