0

I'm facing off an issue due to companion object inside a Realm entity.

Example:

// Entity
open class MyEntity(
    @PrimaryKey
    var id: String? = "",
    // other fields...
): RealmObject() {

    companion object{
        fun clean(realm: Realm) {
            realm.beginTransaction()
            realm.where(MyEntity::class.java).findAll().deleteAllFromRealm()
            realm.commitTransaction()
        }
    }
}

// Migration code
schema
        .create(MyEntity.class.getSimpleName())
        .addField("id", String.class, FieldAttribute.PRIMARY_KEY);

addMissingFields(schema, MyEntity.class);

When migration is executed, I get the following exception:

Realm doesn't support this field type: Companion(class com.example.MyEntity$Companion)

I'd like to know how ignore the companion object from Realm scan.

  • I had similar problem with kotlin companion object, @Ignore annotation seems not to work with methods. So What I did was, I move the method in the companion object outside the class but in the same file then remove the companion object block. Everything seems to work fine after that. – ilatyphi95 Mar 05 '23 at 20:24

1 Answers1

0

Use Realm's annotation @Ignore.

sdex
  • 3,169
  • 15
  • 28