Model cannot be cast to ModelRealmProxyInterface

Viewed 210

I'm trying to use Realm-java library built from source, I have used the below JARs and AARs

  • a jar file for the Realm Gradle plugin
  • an aar file for the Realm library
  • a jar file for the annotations
  • a jar file for the annotations processor

I have added Realm-annotations-processor as below in dependencies

kapt project(':realm-annotations-processor-5.8.0')

When we do

val realm = Realm.getDefaultInstance()
try {
  realm.executeTransaction { realmInstance ->
    realmInstance.copyToRealm(user)// ClassCastException thrown
  }
} finally {
  realm.refreshAndClose()
}

The precise exception we have encountered is:

UserModel cannot be cast to io.realm.com_example_mobile_test_auth_model_UserModelRealmProxyInterface
2 Answers

Finally Solved this myself , I had missed registering the Realm-Transformer which is responsible for generating the RealmProxy.

We need to add below code to build.gradle of required module/app

buildscript {

   repositories {
       jcenter()
       mavenCentral()
   }

   dependencies {
       classpath "io.realm:realm-transformer:5.8.0"
   }
}
import io.realm.transformer.RealmTransformer
android.registerTransform(new RealmTransformer(rootProject))

I had this problem, I tried many solutions, but only this

realm.deleteAll();

could help me

Related