Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance / Polymorphism #761

Open
cmelchior opened this issue Jan 19, 2015 · 192 comments
Open

Inheritance / Polymorphism #761

cmelchior opened this issue Jan 19, 2015 · 192 comments
Labels
Blocked This issue is blocked by another issue Design-Required T-Feature

Comments

@cmelchior
Copy link
Contributor

cmelchior commented Jan 19, 2015

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();
@T-Spoon
Copy link

T-Spoon commented Jan 20, 2015

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!

@cmelchior
Copy link
Contributor Author

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 :)

@carotorrehdz
Copy link

👍

@msegers
Copy link

msegers commented May 12, 2015

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.

@cmelchior
Copy link
Contributor Author

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.

@nolanamy
Copy link

nolanamy commented Jul 3, 2015

@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?

@AliSoftware
Copy link

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?

@emanuelez
Copy link
Contributor

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.

@nolanamy
Copy link

nolanamy commented Jul 8, 2015

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

@rovkinmax
Copy link

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?

@StErMi
Copy link

StErMi commented Sep 24, 2015

Hi, is there any update about this issue?

@afsalashyana
Copy link

I am also eagerly waiting for this feature.

@muxammed
Copy link

I'm also ))

@malwinder-s
Copy link

Is there any update on this?

@Zhuinden
Copy link
Contributor

Zhuinden commented Feb 17, 2020

You can check the road map where it is mentioned that it will happen one day.

(I am not a member of Realm)

@vernazza
Copy link

And here we are, at 2 years and a half since "I can promise that it won't take another year." (#761 (comment))
1 year and a half since "it will (still) take us months (though not years) to do it" (#761 (comment))

sigh :(

@ianpward
Copy link

hey all, product from Realm here, I am in the middle of writing the production description for this feature right now. As you might have guessed since almost no other databases exposes polymorphic inheritance, but rather leaves it up to the developer to build themselves, this is an incredibly difficult feature to build - but rest assured we are planning to build it. As mentioned in some of the comments above we had other high priority tasks we needed to complete first which would lay the foundation before inheritance - for instance we have released Cascading Deletes and are working on Maps and Mixed types right now which would be necessary for Polymorphism. Stay tuned.

@J-Rojas
Copy link

J-Rojas commented Apr 14, 2021

Not sure why people still choose to use Realm with its heavy-handed restrictions. I'm forced to deal with it because of legacy code.

ObjectBox is NoSQL and has supported inheritance for 2 years. You can also use the object across threads, in contrast to Realm.

https://docs.objectbox.io/advanced/entity-inheritance

@tamimattafi
Copy link

hey all, product from Realm here, I am in the middle of writing the production description for this feature right now. As you might have guessed since almost no other databases exposes polymorphic inheritance, but rather leaves it up to the developer to build themselves, this is an incredibly difficult feature to build - but rest assured we are planning to build it. As mentioned in some of the comments above we had other high priority tasks we needed to complete first which would lay the foundation before inheritance - for instance we have released Cascading Deletes and are working on Maps and Mixed types right now which would be necessary for Polymorphism. Stay tuned.

Any updates on this?

@httyd98
Copy link

httyd98 commented Aug 27, 2021

In 2021 Inheritance / Polymorphism is not implemented yet. I have just switched to ObjectBox. Good job!

@kanikaverma7
Copy link

I still don't feel like unsubscribing to this thread. I think I have developed some nostalgic connection with this thread now. Whenever it is revived by a new comment, it puts a smile on my face..and in my mind "Cause we were just kids when we fell in love" :')

@J-Rojas
Copy link

J-Rojas commented Aug 27, 2021

@kanikaverma7 love that last comment!

As another alternative, we choose this little library for building a simple serialized and indexed object cache on disk. You can serialize your model objects using GSON and have all the inheritance you want. Super lightweight and works great for most simple caching use cases.

https://github.com/kittinunf/Fuse

@ianpward
Copy link

ianpward commented Aug 28, 2021

Sorry I should hav posted an update to this ticket as soon as we had new information. We have finished the Initiative writeup and it has been approved internally - but Inheritance is just one part of this Initiative, there are still a couple projects we need to implement before we start Inheritance. One of these migrations, you can imagine with Polymorphism that migrations will become a lot more complicated and we'd like to overhaul this feature so that users can start using Inheritance in production on Day 1. Another is we need to add some features to MongoDB Realm Schema so that our sync users can enjoy this feature too - for instance, we need to add the ability for $anyOf, $allOf, and $oneOf to our JSON schema support (which is how we plan to model Inheritance on the server-side). Polymorphism is actually utilized extensively in MongoDB so we expect sync clients to be some of our biggest users of Inheritance.

We also need to wait for the Android team to have some free cycles. If you're not aware, the Android team has been heads down on building on a purely Kotlin SDK.
https://github.com/realm/realm-kotlin

This comes with Multiplatform support out of the box and since we are building the SDK from the ground up we've taken this opportunity to address some longstanding functionality that has turned off some users. Most notably, the thread confinement of Realm Objects - this becomes an issue for developers who are used to programing with Coroutines natively.
This is why with this new Realm Kotlin SDK, we have removed this thread-confinement restriction and will have first-class support for Coroutines and Flows with built in APIs. It seems to be resonating with early users of our SDK:
realm/realm-kotlin#377

And if you will at the upcoming DroidCon's please look out for a Realm presentation or booth diving deep into our new Kotlin SDKK - stay tuned!

So we have our hands full - I realize that this has been an issue for several years now but we've had a few "small" things happen in our business and technology space that we've had to prioritize - but we have now officially added it to our medium term roadmap and internally, we look forward to working on this and delivering it for our community.

@thipokch
Copy link

thipokch commented Apr 26, 2022

To save some time for those who's looking to move from Java to Kotlin SDK, it seems that neither inheritance nor delegation works. Here's a simple test I did. realm/realm-kotlin#801

If only I had known this sooner.

@afsalashyana
Copy link

Hi!

This is kinda nostalgic thread now. Any updates on this feature? Anything in the pipeline related to inheritance and polymorphism?

@tamimattafi
Copy link

I decided to unsubscribe to this thread. We had to come up with something to support polymorphism in our SQLite database (Using Room for android atm), and it worked. A little more boilerplate code, but it does the job.

@Zhuinden
Copy link
Contributor

Zhuinden commented Jul 9, 2022

Okay, no, if that's what you need then the only thing you can do atm is you emulating polymorphism via single-table-inheritance-with-discriminator-field.

Meaning you have 1 big RealmObject that can possibly contain all fields of all "inheriting" objects and you also have a field called something like type or objectType or whatever which is most likely a String that is indexed and then you can query the data of a given type based on that

This approach worked back in 2015 and still works to this day btw

@NizarETH
Copy link

Any updates on this feature?

@marchy
Copy link

marchy commented May 28, 2023

@ianpward would you mind giving a "State of the Union" update on Realm inheritance?

Your last such update in Summer 2021 was extremely helpful. We are looking to make some large architecture decisions based on inheritance support (in the Swift client for now, but Android just the same), so would be great to feel out a realistic timeframe for this – ie: is it imminent / expected this year VS not.

Cheers!

@vagrant1991
Copy link

can't believe this issue still open

@dhng22
Copy link

dhng22 commented Jan 20, 2024

Almost 10 years for a feature, it seems like the team still has other priorities to concern. To me, realm is still an immature library to use. This issue is just one of those basic features that are being requested out there.

@JosephHarvey-Xamarin
Copy link

Almost 10 years for a feature, it seems like the team still has other priorities to concern. To me, realm is still an immature library to use. This issue is just one of those basic features that are being requested out there.

Inheritance / Polymorphism is a core concept of modern software development. Do they have a timeline?

@longbombus
Copy link

I am glad that I encountered this problem at the start of using Realm. What is the benefit of NOSQL if the table fields are fixed as in SQL?
Now I'm looking for an alternative.

@Zhuinden
Copy link
Contributor

@longbombus I'd be a little more concerned about the fact that the database is now fully abandoned by MongoDB (see the Atlas Device SDK deprecation docs)

Which is sad, technically it was pretty easy to do Inheritance, you did it manually with a discriminator field. Sure, it definitely wasn't first-party, but the workaround was doable. I dislike issues that have no workarounds. And stability concerns. Still, there was a lot of potential, but also a lot of potential lost to the 5 iterations of Realm Sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked This issue is blocked by another issue Design-Required T-Feature
Projects
None yet
Development

No branches or pull requests