Java – How do I convert this model to a valid RealmObject?

How do I convert this model to a valid RealmObject?… here is a solution to the problem.

How do I convert this model to a valid RealmObject?

I’m new to Realm and I want to stick with the following model:

public class ChangeEntry {

private int id;
    private long time;
    private boolean active;
    private Set<Change> changes;

 getters and setters
}

Change is an interface (I have multiple implementations):

public interface Change {

void performChange();
}

As far as I know, Realm doesn’t support Sets, so I made the following changes:

  • Switch from Set <Change > to RealmList<Change>
  • Make the ChangeEntry extension RealmObject
  • Make the Change extension RealmModel

Now when I try to save ChangeEntry to Realm, I get the following error:

Only concrete Realm classes are allowed in RealmLists. Neither
interfaces nor abstract classes are allowed.

This is self-explanatory.

Is it possible to save this model to Realm and keep Change's polymorphic interface provided? If yes, what to do?

Thanks in advance.

Solution

Is it possible to save this model to Realm and preserve the polymorphism that the Change interface provides?

Currently not supported because realm does not yet support polymorphism. It is being processed and you can track the progress here

Now you can only use concrete classes.

Related Problems and Solutions