Best Practice And How To Implement Realmlist That Need To Support Different Types Of Objects
I have a model 'A' that have a list that can be of type 'B' or 'C' and more. I know Polymorphism is not supported by Realm and i cant just do RealmList or RealmL
Solution 1:
Polymorphism support is tracked here: https://github.com/realm/realm-java/issues/761 , but as long as it isn't implemented you have to use composition instead (https://en.wikipedia.org/wiki/Composition_over_inheritance)
In your case it would look something like this:
publicinterfaceMyContract {
int calculate();
}
publicclassMySuperClassextendsRealmObjectimplementsMyContract {
private A a;
private B b;
private C c;
@Overridepublic int calculate() {
returngetObj().calculate();
}
privateMyContractgetObj() {
if (a != null) return a;
if (b != null) return b;
if (c != null) return c;
}
publicbooleanisA() { return a != null; }
publicbooleanisB() { return b != null; }
publicbooleanisC() { return c != null; }
// ...
}
Post a Comment for "Best Practice And How To Implement Realmlist That Need To Support Different Types Of Objects"