realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Inheritance / Polymorphism

Open cmelchior opened this issue 10 years ago • 192 comments

A highly request feature is polymorphism. Mostly for use in ListAdapters, but a lot of other scenarios exists.

We need support for the following:

  • Abstract RealmObject classes
  • Query support

Example

public abstract Animal extends RealmObject {
  private String name;
}

public class Dog extends Animal {
  private boolean willPlayCatch;
}

public class Spider extends Animal {
  private boolean isScary;
}

// Following should be possible
RealmResults<Animal> animals = realm.where(Animal.class).equalsTo("name","Foo").findAll();
String name = animals.get(0).getName();

RealmResults<Dog> dogs = realm.where(Dog.class).equalsTo("name","Foo").and().equalsTo("willPlayCatch", true).findAll();
String name = dogs.get(0).getName();

cmelchior avatar Jan 19 '15 07:01 cmelchior

Any estimate on a time frame for this? I'm hoping to use Realm in an upcoming project, but without support for Model inheritance I'll have to hold off on trying it out.

Looking forward to where you guys take this project though!

T-Spoon avatar Jan 20 '15 22:01 T-Spoon

Hi @T-Spoon We have a couple of other major features we would like to look at first, so we don't have a timeframe yet, sorry. If inheritance is a dealbreaker for you, you should probably not use us this time around. But we do hope you will come back :)

cmelchior avatar Jan 21 '15 05:01 cmelchior

:+1:

carotorrehdz avatar Feb 03 '15 22:02 carotorrehdz

Still no estimates? Going to write a lot of duplicate code since I want to use Realm anyways. Works fine in swift already. Monitoring this.

msegers avatar May 12 '15 10:05 msegers

Unfortunately not. The support for this in Objective C and Swift is actually just sharing fields. It is not proper polymorphism. This is good enough for some use cases, but we feel that without being able to query on abstract super classes it will be less useful on Android. Right now we are focusing on migrations, null support and async querying, but hopefully we will be able to tackle this afterwards.

cmelchior avatar May 12 '15 11:05 cmelchior

@cmelchior So is there any way to share fields on Android? We have 5 models that all share the same synchronization-related fields and code. We use inheritance in our current SQLite models; if we switch to Realm, will we have to duplicate the synchronization fields across each of the 5 model classes?

As a workaround, I'm thinking about having those classes implement an interface with getters and setters for the shared fields, which at least let me share the synchronization code... is there a better way?

nolanamy avatar Jul 03 '15 00:07 nolanamy

While waiting for this inheritance feature to be available, is there any workaround, like a good practice to design our RLMObjects and model to make such use cases possible?

My need is with a typical object model having, say, Animals objets, subclassed as Dogs and Cats, and I want sometimes to fetch all the Animals (to display in a List only common properties of those animals like their name), and sometimes (e.g. when I tap on an item in the list to show the details) to access properties of the specific class Dog or Cat.

Given that I can't use inheritance to represent that in Realm yet, how would you guys represent your data model instead to make this use-case possible? Adding a relationship from the Dog and Cat RLMObjects to the Animal RLMObject (composition instead of inheritance) maybe?

AliSoftware avatar Jul 08 '15 12:07 AliSoftware

The current solution is to use composition instead of inheritance. There are lots of good argument about it (Gang of Four and Joshua Bloch to mention some prestigious supporters of this approach).

On the other hand we are considering to allow inheritance, but it would not allow queries. For example: Animal which is extended both by Dog, Cat and Duck. You would not be able to query Animals with for legs and have all Dogs and Cats, but not Ducks. We feel this would be a very crippling behavior but are eager to listen to more opinions.

emanuelez avatar Jul 08 '15 12:07 emanuelez

@emanuelez Is @AliSoftware's composition-over-inheritance example what you have in mind?

nolanamy avatar Jul 08 '15 18:07 nolanamy

See other model: Im have base object

public abstract class BaseObject {
    private String guid;

    private Map<String, String> name;

    private List<String> tags;

    private double latitude;

    private double longitude;

    private LatLng position;

    @Nullable
    public String getName(String locale) {
        if (name != null) {
            return name.get(locale);
        }
        return null;
    }

    public String getName() {
        return getName(getDefaultLocale());
    }

}

and some other child

public class City extends BaseObject {

    int version;

    boolean current = false;
}
public class Station extends BaseObject {

    Map<String, String> next;

    List<String> routes;

    private List<String> bus;
    private List<String> shuttle;
    private List<String> tram;
    private List<String> troll;
}
public class RoutePoint extends BaseObject {
    int direction;
    String routeGuid;
}

and few other BaseObject contains also equals and hashCode methods How much do I need to copy paste the code and how much it will generate errors?

rovkinmax avatar Sep 22 '15 10:09 rovkinmax

Hi, is there any update about this issue?

StErMi avatar Sep 24 '15 20:09 StErMi

Hi @StErMi We are currently focusing on finishing other features, and we cannot move forward with this until we made changes to the underlying storage engine as it must support querying across abstract types. At this time I am afraid I don't have any timeline.

cmelchior avatar Sep 25 '15 07:09 cmelchior

@cmelchior, I can help you guys building this feature but I need little help from you guys in order to get starting so if you can guide me with the kick off process that would be great.

srikalyan avatar Oct 28 '15 16:10 srikalyan

I appreciate your hard work on this excellent piece of software but it's a big pain in the ass not being able to use inheritance as we do in swift. I have a big model in swift full of inheritance that I have to implement in Android and now I realize this is not allowed...I never imagined such a restriction could exist on a object oriented language component.

IMO this is a very important thing but this ticket was opened for almost a year without any estimated timeline so I assume the solution is not near :)

claudioredi avatar Nov 04 '15 21:11 claudioredi

Unfortunately we don't have a timeline yet. We have a long list of features we would like to do, and we are continuously evaluating the order we do them in, so feedback such as yours is much appreciated.

Note that Swift offers inheritance but not polymorphism, which we think would be the expectation for any Java developer for this feature. So for now we recommend using composition instead of inheritance.

cmelchior avatar Nov 04 '15 21:11 cmelchior

@srikalyan Sorry for the late reply. Thank you for your offer, but this feature unfortunately require changes to the underlying core engine (all issues marked "waiting-for-core" does that), but if you find any issues without that label, we would love to start a discussion on them if you want to help.

cmelchior avatar Nov 04 '15 21:11 cmelchior

@cmelchior Just echoing the desire to have polymorphic relationships supported in Realm. Also, some polymorphic solutions seem to be Integer focused; it would be good to have something that supports Binary and String/Hex as well.

Until Realm releases polymorphic support, we're building workarounds at the cost of performance, code efficiency, and model simplicity.

mlusas avatar Nov 24 '15 21:11 mlusas

IMO this is a very important thing but this ticket was opened for almost a year without any estimated timeline so I assume the solution is not near :)

I've used Realm for quite a few projects. And having inheritance would save me a lot of trouble. Like someone said before, this ticket was opened a year ago. Realm is my fav library out their for database related stuff in Android. And I hope this feature will be added soon, I think Realm could be a big player in how DB related things get handled in Android. It is great as it is, but missing inheritance in a OOPL like java I think it is a major let down for a lot of people.

I assume there is still no estimated timeline?

Keep up the good work though!

nomisRev avatar Dec 30 '15 08:12 nomisRev

:+1:

rpip avatar Jan 07 '16 12:01 rpip

+1

savekirk avatar Jan 07 '16 13:01 savekirk

+1 Hopefully this feature is still on the table

borwoj avatar Jan 19 '16 11:01 borwoj

+1000

dbauduin avatar Jan 19 '16 11:01 dbauduin

+1! :)

wzieba avatar Jan 30 '16 20:01 wzieba

+1 plz

monossido avatar Feb 02 '16 12:02 monossido

+1

eyecreate avatar Feb 03 '16 18:02 eyecreate

+1

yosrah avatar Feb 29 '16 11:02 yosrah

+1

quangson91 avatar Mar 01 '16 08:03 quangson91

+1

mformetal avatar Mar 05 '16 21:03 mformetal

+1

kmahmood74 avatar Mar 06 '16 22:03 kmahmood74

+1

milosgh avatar Mar 16 '16 11:03 milosgh