Can I define a field with unknown type in Realm model?
Sample classes :
public class Model1 extends RealmObject {
@PrimaryKey
private String _id;
private ? field1;
}
public class Model2 extends RealmObject {
@PrimaryKey
private String _id;
}
public class Model3 extends RealmObject {
@PrimaryKey
private String _id;
}
Now, the field1 in Model1 can be of type Model2 or Model3 which will be determined during run time. Is there any way I can achieve this?
No, you cannot do that. Dalinaum's comment is correct.
One way to achieve it is like;
public class Model1 extends RealmObject {
@PrimaryKey
private String _id;
private Model2 model2;
private Model3 model3;
}
public class Model2 extends RealmObject {
@PrimaryKey
private String _id;
}
public class Model3 extends RealmObject {
@PrimaryKey
private String _id;
}
and access it via;
if (model1.getModel2() == null) {
Model2 model = model1.getModel2()
// do something
} else {
Model3 model = model1.getModel3()
// do something
}