-
Notifications
You must be signed in to change notification settings - Fork 87
Open
Labels
Description
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:
- Copy the default.realm file to a new file called sandbox.realm
- Try to open the sandbox Realm and run the required data migration
- Close the Realm and delete the sandbox file
- Open the default Realm normally
- 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
)
}