Skip to content

Abstract Class / Polymorphism Support #1109

Open
@Kirow

Description

@Kirow

Does it possible to create smth like this:
Abstract Data Model

Blue - abstract classes. Such model will help me to incapsulate some logic and prevent code duplication.

In this example RLMItem can be associated with RLMCategory or/and RLMFolder. It would be very useful if i will be able to get all RLMFolders and RLMItems using [RLMCatItem allObjects].
Seems that it is not possible.

I've tried to make smth similar with subclasses, but as result - I have additional useless classes in schema.

This works well with CoreData, but can I expect smth like this in Realm?

_In other words:_

  1. Abstract classes will not be displayed in Realm Browser
  2. Ability to query abstract classes (this will include all subclass objects)

Activity

jpsim

jpsim commented on Nov 11, 2014

@jpsim
Contributor

Hi @Kirow, there's definitely value in your modeling approach, but Realm's current inheritance implementation doesn't allow for the polymorphism you're looking for.

Your first goal would be fairly easy to support (avoiding creating unused tables in the db). However, empty tables in Realm are very small so even though this is annoying, it shouldn't have any significant performance or usability impact. If you're keen on filtering which tables are created in Realm, you could patch RLMSchema's +initialize method.

The second goal highlights a more general sore point in Realm's architecture, which is that containers for a class (RLMResults or RLMArray) can't contain instances of its subclass. So for this reason, we can't include subclasses in [RLMObject allObjects] for now.

So for the time being, you'll have to adapt your model to fit with Realm's inheritance approach. Meanwhile, we'll keep an eye on possible modeling improvements for Realm and we'll make sure to update this thread when we have anything to share.

raspu

raspu commented on Nov 20, 2014

@raspu

I am having a similar issue to @Kirow's, I have an RLMArray defined to store an abstract class, and storing there subclasses of the abstract class, but of course is giving me back instances of the abstract class.

It will be really awesome if Realm supports this in the future.

puttin

puttin commented on May 21, 2015

@puttin

realm/realm-java#761 is related issue

changed the title [-]Abstract Classes Support[/-] [+]Abstract Class / Polymorphism Support[/+] on Jun 23, 2015

48 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wanbok@alazier@jpsim@raspu@eanagel

        Issue actions

          Abstract Class / Polymorphism Support · Issue #1109 · realm/realm-swift