I need to store information on certain classes, which, in turn, are inherited from other classes.

As far as I know, in 2015, Realm developers wrote that while inheritance is not supported, but its support is planned in the future.

I found information from the middle of 2016, which says that inheritance is not yet supported.

On the current version of Realm, I cannot save inherited objects, but maybe I just do something wrong?

Tell me, please, how is Realm now with inheritance?

If there is still no support for inheritance, can there be any less difficult ways to solve this problem (without drastically changing the existing model level in general and without decomposing it in particular)?

I have one solution, but I would like to know how you solve this problem.

  • It is written in the documentation ( realm.io/docs/java/latest/#field-types \) that only simple types, String , Date and RealmObject / RealmList / are supported as Realm model fields. Accordingly, no other classes are inherited or not. can be model fields in this db. Or I did not understand the question ... - pavlofff
  • @pavlofff, I'm not talking about fields. I meant the following: let there be class A {...} and class B extends A {...} , how to save objects of class B to Realm? - post_zeew
  • That is, use class B as a model that is inherited, but not from RealmObject or “put” class B as one of the fields in the RealmObject model? - pavlofff
  • @pavlofff, first. - post_zeew
  • one
    @pavlofff, I tried this: class A {...} , class B extends A implements RealmModel {...} . As a result, when compiling, I get the error: Realm model classes must either extend RealmObject or implement RealmModel to be considered a valid model class (refers to class B ). - post_zeew

1 answer 1

Rilm has support for implementing RealmModel instead of inheriting from RealmObject by the model class. Please note that you need to add the @RealmClass annotation, maybe that's why it didn't work ... Just your case?

But generally speaking, over a year of using Rilma in production projects, we never had such a problem that we had to implement RealmModel. Perhaps it is worth revising the class hierarchy? Models are database tables, maybe it’s still not worthwhile for them to inherit some other objects and acquire additional behavior not related to the database? What if you replace inheritance with your class A interface implementation?

 class B extends RealmObject implements A { . . . // your fields . . . } 

Simply, well, what is it about in this class A, in order to actually inherit it and not implant the interface?

  • I know about this interface, but when it is implemented, errors appear during the compilation process (see the comments to the question). The class annotated. I have many ways to solve this problem, but I would like to understand the direct solution that Realm provides. - post_zeew
  • It is very strange. Have you tried on a clean class that does not inherit anyone?? Do you use Rilma modules? Schemes? sometimes this is important. - Vladymyr R.tmnko
  • Yes, I tried. Created a clean project with the addition of one simple class, it was still not compiled. - post_zeew
  • Well, it was the modules of the Rilma that were prescribed, where are the classes that are included in the module described? And you need to add the necessary modules to the scheme ... more detailed on realm.io in the section Configuring a Realm - Vladymyr R.tmnko
  • No, the default configuration was used. Do you think this is the case? There is nothing written about this here . - post_zeew