Realm-java: Cannot be cast to io.realm.FooRealmProxyInterface

Created on 16 Dec 2016  路  24Comments  路  Source: realm/realm-java

Goal

Simply load data, I'm using Kotlin 1.0.4 and minifyEnabled

Result

ClassCastException when it tries to do a query.

Realm error

Steps & Code to Reproduce

It happens randomly, the code sample is below.

Code Sample

fun insertOrUpdate(taskQueue: TaskQueue?, realm: Realm?) {
  try {
    realm?.run {
      executeTransaction {
        taskQueue?.let {
          insertOrUpdate(it)
        }
      }
    }
  } catch (e: Exception) {
    if (DEBUG_ENABLED) {
      e.printStackTrace()
    }
    Crashlytics.logException(e)
  }
}
open class TaskQueue(
    @PrimaryKey var id: String,
    var layers: RealmList<Layer> = RealmList()):
    RealmObject(), Parcelable {

  companion object {
    @JvmField val CREATOR = object : Parcelable.Creator<TaskQueue> {
      override fun createFromParcel(source: Parcel) = TaskQueue(source)
      override fun newArray(size: Int): Array<TaskQueue?> = arrayOfNulls(size)
    }
  }
  //...Parcel and more not important
}

Version of Realm and tooling

Realm version(s): 2.2.1
Realm sync feature enabled: No
Android Studio version: 2.1.2 / gradle-2.14.1
Which Android version and device: 6.0 - LG G2

T-Help

Most helpful comment

Does adding @RealmClass annotation to TaskQueue class solve the issue? https://realm.io/docs/java/latest/#kotlin

All 24 comments

This is not the relevant code sample, the code sample would be when insertOrUpdate is called in a lambda expression.

It sounds like a List is saved in which some objects are managed and some are not.

It looks strange, the TaskQueue (both managed/unmanged) should be able to be cast to TaskQueueRealmProxyInterface always because Realm transformer did it, see addRealmProxyInterface of Realm transformer.

@ppamorim Are you using instant run? https://realm.io/docs/java/latest/#kotlin is any information here useful? Can you reproduce the issue with a minimal project?

@Zhuinden I'm doing only that insert, nothing different.
@beeender No, I'm not. I had a lot of problems (the project doesn't compile using Instant run), then it is disabled.

Here is the error:

screen shot 2016-12-19 at 11 28 28

Have the same problem with copyOrUpdate.

I use this approach:

fun cache(realm: Realm): CachedIdRealmObject {
    cacheTransform?.invoke()

    return if (realm.getTable(this.javaClass).hasPrimaryKey())
        realm.copyToRealmOrUpdate(this)
    else realm.copyToRealm(this)
}

CachedIdRealmObject is just an interface, extends RealmModel

Problem remains with top-level models with @PrimaryKey-s and with @PrimaryKey models inside non-@PrimaryKey models.

The problem is at this line:

long rowIndex = table.findFirstLong(pkColumnIndex, ((FileModelInfoRealmProxyInterface) object).realmGet$id());

Kotlin: 1.0.5-3 / plugin: 1.0.6-eap-112
Realm: 2.2.1
AS: 2.3 Beta 1
Gradle: 3.2
Android Gradle plugin: 2.3.0-beta1

@altero Are you using Proguard or Minify? If yes, try to disable it for a moment. I'm doing some tests here and I noticed that error is gone when it is disabled.

Ok, it seems to work great with

Kotlin: 1.0.5-3 / plugin: 1.0.5-3
Realm: 2.2.1
AS: 2.2
Gradle: 3.2
Android Gradle plugin: 2.2.3

Not sure what actually fixed the build, but anyway.

@ppamorim I tried to build with and without proguard. It seems, that proguard was not the problem, coz without proguard I also had this error.

@altero False alarm, this error is happening with Proguard disabled, sad. :(

Kotlin: 1.0.5-3 / plugin: 1.0.5-3
Realm: 2.2.1
AS: 2.2
Gradle: 3.2
Android Gradle plugin: 2.2.3

Sorry, this doesn't work either, continue to downgrade environment versions...

you apply kotlin-android before realm-android, right?

@Zhuinden

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'com.getkeepsafe.dexcount'

Well, I totally forgot. I'm using kapt2

But everything worked until I've updated smth to alpha/canary/eap version.

@Zhuinden

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'checkstyle'
apply plugin: 'realm-android'

I'm using kapt2 too.

Do you think you can try kapt instead of kapt2 and see if it surfaces?

Although I see no reason why it wouldn't work.

Ok, I found working configuration:

Kotlin: 1.0.5-3 / plugin: 1.0.5-3
Realm: 2.2.1
AS: 2.2
Gradle: 3.1
Android Gradle plugin: 2.2.3

And I remembered, that I had same problems last time I updated Gradle to 3.2. I believe, that the problem is with it and it's now a default for Android Studio 2.3.

@Zhuinden it seems, that @ppamorim uses kapt, instead of kapt2. Coz there is no apply plugin: 'kotlin-kapt' in his configuration.

Does adding @RealmClass annotation to TaskQueue class solve the issue? https://realm.io/docs/java/latest/#kotlin

@altero Sorry I'm using kapt2 I just missed the apply plugin hehe
@zaki50 I will check it.

@zaki50 doesn't work for me. Still get

java.lang.ClassCastException: pa.ckage.data.models.FileModelInfo cannot be cast to io.realm.FileModelInfoRealmProxyInterface

after adding @RealmClass with Gradle 3.2.
On Gradle 3.1 works ok even without @RealmClass.

Hmm, I tried to reproduce it on my project, but no success.

https://github.com/zaki50/realm_template/blob/master/kotlin_app/build.gradle
I executed realm.insertOrUpdate() on this project and it completely worked...
Can you see any differences in my project?

I'm closing for now. If you are able to spot any differences between your project and @zaki50's project, please tell us to reopen.

@kneth What about this?

gradle.properties

org.gradle.parallel=true

App module A depends on Library module B. Both have Realm classes, both apply the Realm plugin.

Realm 3.0.0, Kotlin 1.1.1 with kapt2/3, kotlin.incremental=false.

Maybe @zaki50 can try to various combinations of those flags?

Had the same problem. It was solved by 3 steps: 1. Clean the project 2. Clear app data/cache 3.Uninstall/install app

Was this page helpful?
0 / 5 - 0 ratings