-
Type: Improvement
-
Resolution: Incomplete
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
How frequently does the bug occur?
Always
Description
I have one type of object in Realm schema 1, for example - "Album". Later, I have decided to separate some "Album"s into "Playlist"s in schema 2.
The class for "Album" is, for example, like this:
class Album: RealmObject { @PrimaryKey val id: String val title: String val tracks: RealmList<Track> }
And for "Playlist", is like this:
class Playlist: RealmObject { @PrimaryKey val id: String val title: String val tracks: RealmList<Track> val author: String }
Because you cannot change the object type while migrating, I have decided to
- convert the old data from old realm into a DynamicMutableRealmObject
- delete the new "placeholder" using delete in newRealm which is get inside the enumerate of a MigrationContext
- insert the converted old data by using copyToRealm
But if I specify Realm objects inside DynamicMutableRealmObject.create, it will say that I need first to find the latest version of an object by using the findLatest function. It works for embedded objects or variables, but it does not work for RealmLists.
If I map "old" objects in a List to managed objects in RealmList using findLatest, I will get an error when inserting that "class io.realm.kotlin.internal.dynamic.DynamicMutableRealmObjectImpl not part of this configuration schema". This can be solved by manually copying data from old object and creating a new "scratch object", but that will involve a lot of boilerplate code because of having over 15 variables in real code - so I am looking for a way to solve this.
Interestingly, looking in the code itself I can see that it can handle such values:
override fun createInstanceOf(clazz: KClass<out BaseRealmObject>): RealmObjectInternal = when (clazz) { DynamicRealmObject::class -> DynamicRealmObjectImpl() DynamicMutableRealmObject::class -> DynamicMutableRealmObjectImpl() DynamicUnmanagedRealmObject::class -> DynamicMutableRealmObjectImpl() else -> companionOf(clazz).`io_realm_kotlin_newInstance`() as RealmObjectInternal } override fun companionOf(clazz: KClass<out BaseRealmObject>): RealmObjectCompanion = mapOfKClassWithCompanion[clazz] ?: error("$clazz not part of this configuration schema")
It creates a DynamicRealmObjectImpl out of DynamicRealmObject, but crashes if DynamicRealmObjectImpl is provided beforehand.
Thanks!
Stacktrace & log output
Caused by: java.lang.IllegalStateException: class io.realm.kotlin.internal.dynamic.DynamicMutableRealmObjectImpl not part of this configuration schema at io.realm.kotlin.internal.ConfigurationImpl$3.companionOf(ConfigurationImpl.kt:243) at io.realm.kotlin.internal.ConfigurationImpl$3.createInstanceOf(ConfigurationImpl.kt:238) at io.realm.kotlin.internal.RealmObjectUtilKt.toRealmObject(RealmObjectUtil.kt:87) at io.realm.kotlin.internal.RealmObjectHelper.dynamicSetValue$io_realm_kotlin_library(RealmObjectHelper.kt:2450) at io.realm.kotlin.internal.RealmObjectHelper.dynamicSetValue$io_realm_kotlin_library$default(RealmObjectHelper.kt:1023) at io.realm.kotlin.internal.dynamic.DynamicMutableRealmObjectImpl.set(DynamicMutableRealmObjectImpl.kt:134) at io.realm.kotlin.dynamic.DynamicMutableRealmObject$DefaultImpls.set(DynamicMutableRealmObject.kt:61) at io.realm.kotlin.internal.dynamic.DynamicMutableRealmObjectImpl.set(DynamicMutableRealmObjectImpl.kt:26)
Can you reproduce the bug?
Always
Reproduction Steps
- Create a model with a RealmList of another model
- Create an another model with a RealmList
- Update the schema version
- In a migration, try to create a model 2 out of model 1 data, while specifying the RealmList variable value by using getObjectList + map(realm::findLatest) + toRealmList()
- The migration will crash when calling copyToRealm
Version
1.10.1
What Atlas App Services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
Android 13
Build environment
Android Studio version: 2023.1.1 Canary 10
Android Build Tools version: 30.0.3, AGP is 7.2.2
Gradle version: 7.6