Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-866

DynamicMutableRealmObjectImpl retrieved by findLatest results in crash when migrating

      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:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class Album: RealmObject {
        @PrimaryKey val id: String
        val title: String
        val tracks: RealmList<Track>
      }
      

      And for "Playlist", is like this:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
                  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

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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

      1. Create a model with a RealmList of another model
      2. Create an another model with a RealmList
      3. Update the schema version
      4. 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()
      5. 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

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: