realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ClassCastException: UnmanagedRealmList cannot be cast to ManagedRealmList

XilinJia opened this issue · comments

How frequently does the bug occur?

Always

Description

object RealmWorker {
    val realm: Realm
    init {
        val config = RealmConfiguration.create(
            schema = setOf(FeedR::class, FeedItemR::class, FeedMediaR::class)
        )
        realm = Realm.open(config)
    }
}

class FeedR : RealmObject { ... 
    @JvmField
    var items: RealmList<FeedItemR> = realmListOf()

    fun fromFeed(feed: Feed) {
        ...
        this.items = realmListOf<FeedItemR>().apply {
            for (item in feed.items) {
                val itemR = FeedItemR()
                itemR.fromFeedItem(item)
                add(itemR)
            }
        }
    }
}

                        val feedR = FeedR()
                        feedR.fromFeed(result_.feed)
                        realm.writeBlocking {
                            copyToRealm(feedR)
                        }

Stacktrace & log output

java.lang.ClassCastException: io.realm.kotlin.internal.UnmanagedRealmList cannot be cast to io.realm.kotlin.internal.ManagedRealmList
                                                                                                    	at io.realm.kotlin.internal.RealmObjectHelper.assignTyped$io_realm_kotlin_library(RealmObjectHelper.kt:797)
                                                                                                    	at io.realm.kotlin.internal.RealmObjectHelper.assign$io_realm_kotlin_library(RealmObjectHelper.kt:734)
                                                                                                    	at io.realm.kotlin.internal.RealmUtilsKt.copyToRealm(RealmUtils.kt:220)
                                                                                                    	at io.realm.kotlin.internal.RealmUtilsKt.copyToRealm$default(RealmUtils.kt:144)
                                                                                                    	at io.realm.kotlin.internal.InternalMutableRealm$DefaultImpls.copyToRealm(InternalMutableRealm.kt:57)
                                                                                                    	at io.realm.kotlin.internal.SuspendableWriter$WriterRealm.copyToRealm(SuspendableWriter.kt:58)
                                                                                                    	at io.realm.kotlin.MutableRealm$DefaultImpls.copyToRealm$default(MutableRealm.kt:88)
                                                                                                    	at ac.mdiq.podcini.ui.fragment.OnlineFeedViewFragment$parseFeed$1$result$1$1.invoke(OnlineFeedViewFragment.kt:406)
                                                                                                    	at ac.mdiq.podcini.ui.fragment.OnlineFeedViewFragment$parseFeed$1$result$1$1.invoke(OnlineFeedViewFragment.kt:405)
                                                                                                    	at io.realm.kotlin.internal.SuspendableWriter$write$2.invokeSuspend(SuspendableWriter.kt:125)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)


### Can you reproduce the bug?

Always

### Reproduction Steps

_No response_

### Version

SDK 33, 34

### What Atlas App Services are you using?

Local Database only

### Are you using encryption?

No

### Platform OS and version(s)

Manjaro 24.0

### Build environment

Android Studio version: IntelliJ IDEA 2024.1.1 (Community Edition) 
Android Build Tools version: 34
Gradle version: 8.5

BTW, it's my first attempt to integrate Realm in my open source project, hoping to replace SQLite.  If someone suspect something I do could cause this exception, please make some suggestions.

➤ PM Bot commented:

Jira ticket: RKOTLIN-1086

I fooled around a bit. It appears the @JvmField annotation with items is the culprit (not sure why). I took it out and the code runs fine. So it has problem with the one-many relation property. For other regular properties, it doesn't seem to do any harm. So what's the advice on using it in Realm objects?