Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException: UnmanagedRealmList cannot be cast to ManagedRealmList #1756

Closed
XilinJia opened this issue May 21, 2024 · 3 comments
Closed

Comments

@XilinJia
Copy link

XilinJia commented May 21, 2024

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.
Copy link

sync-by-unito bot commented May 21, 2024

➤ PM Bot commented:

Jira ticket: RKOTLIN-1086

@XilinJia
Copy link
Author

XilinJia commented May 21, 2024

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?

@nirinchev
Copy link
Member

I don't believe @JvmField is compatible with RealmObject properties.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants