https://kotlinlang.org logo
Title
i

iamsteveholmes

07/04/2021, 8:28 PM
Hey Folks! Has anyone tried to use #realm with MVIKotlin? I'm using it because my app requires syncing of local and remote data and between users which is a very difficult problem that Realm seems to solve. The problem I've run into is that even adding the Realm drivers as a dependency seems to cause the build to fail. Here are some pieces of the stacktrace:
e: org.jetbrains.kotlin.util.KotlinFrontEndException: Front-end Internal error: Failed to analyze declaration Intent
File being compiled: (9,5) in .../store/CharacterListStore.kt
The root cause java.lang.AssertionError was thrown at: org.jetbrains.kotlin.resolve.lazy.descriptors.AbstractLazyMemberScope.getContributedClassifier(AbstractLazyMemberScope.kt:75)

...then....

Caused by: java.lang.AssertionError: Recursion detected on input: Intent under LockBasedStorageManager@33ccfc53 (TopDownAnalyzer for JVM)

...and later in the stacktrace I find:

at io.realm.compiler.IrUtilsKt.getHasRealmModelInterface(IrUtils.kt:90)
        at io.realm.compiler.RealmModelSyntheticCompanionExtension.getSyntheticCompanionObjectNameIfNeeded(RealmModelSyntheticCompanionExtension.kt:45)
And here is the "offending" code:
interface CharacterListStore : Store<Intent, State, Nothing> {
    sealed class Intent {
        data class Delete(val id: Long) : Intent()
        object Loaded : Intent()
    }

    data class State(
        val characters: List<Character> = emptyList(),
        val selectedCharacterId: Long? = null,
    )
}
a

Arkadii Ivanov

07/04/2021, 8:56 PM
I did not use Real yet. But how the error is related to MVIKotlin? Since the offending code is just an interface, you could try removing any dependency on MVIKotlin and see if the error is still there. I would produce a minimal reproducer and file a bug to Realm.
i

iamsteveholmes

07/04/2021, 11:54 PM
That's a very good point. I forgot to include this chunk of the stacktrace:
e: org.jetbrains.kotlin.util.KotlinFrontEndException: Front-end Internal error: Failed to analyze declaration Intent
But it's still just saying it doesn't like the class declaration as far as I understand which has no dependency on MVIKotlin.
@Arkadii Ivanov Sadly it appears that there is an imcompatibility with Realm and implementing the MVIKotlin Store interface. If I remove the interface from my code it will compile. I've logged a bug on the realm-kotlin repo at: https://github.com/realm/realm-kotlin/issues/339 And here is my sample project: https://github.com/iamsteveholmes/RealmMVIKotlinTest
a

Arkadii Ivanov

07/09/2021, 8:28 PM
I think if you replace MVIKotlin's stuff with your own similar, the error should be still there. It looks like an unlucky type hierarchy that their compiler plugin can't handle.
It is noticeable that there are no Realm objects used in the reproducer project, just regular code. Looks like Realm compiler plugins tries to analyse it and fails. Not sure why does it ever try to analyse the unrelated code.
i

iamsteveholmes

07/09/2021, 8:55 PM
That's a good question and one I've been thinking about. I have the impression that it tries to evaluate all objects to see if they extend "RealmObject" and the evaluation itself causes an exception. I wonder if there would be a way to skip the evaluation or constrain it to avoid this error. Similar errors exist on Kotlin tickets that haven't been resolved. Sadly I haven't seen any movement on the Realm github ticket yet.
a

Arkadii Ivanov

07/09/2021, 9:20 PM
There is also a minor chance of Kotlin version incompatibility. If possible could please try an older version of Realm which is compiled against Kotlin 1.5.0? If it's not too old for you.
No need to check the older version. Here is a minimal reproducer, unrelated to MVIKotlin:
interface CharacterListStore : Store<CharacterListStore.Intent> {
    class Intent
}

interface Store<T : Any>
A workaround is to put the
Intent
class outside of the
CharacterListStore
.
i

iamsteveholmes

07/12/2021, 8:42 PM
Holy Moly! Wow good work!
You saved my Bacon! I tried a couple of things around the imports but didn't consider moving the class out
a

Arkadii Ivanov

07/12/2021, 8:43 PM
Glad that was helpful! Please consider attaching the code to the issue
i

iamsteveholmes

07/13/2021, 1:51 AM
Will do!
a

Arkadii Ivanov

07/13/2021, 6:04 AM
Also you can create a separate Gradle module for your Realm classes. So the Realm compiler plugin will be enabled only in that module.
i

iamsteveholmes

07/16/2021, 8:00 PM
That’s a great idea….