Open
Description
Does it possible to create smth like this:
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:_
- Abstract classes will not be displayed in Realm Browser
- Ability to query abstract classes (this will include all subclass objects)
Activity
jpsim commentedon Nov 11, 2014
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
orRLMArray
) 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 commentedon Nov 20, 2014
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 commentedon May 21, 2015
realm/realm-java#761 is related issue
[-]Abstract Classes Support[/-][+]Abstract Class / Polymorphism Support[/+]48 remaining items