Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Realm fields on Android

Realm on Android doesn't support model inheritance/polymorphism.

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, is our only choice to duplicate the sync fields across each of the 5 model classes?

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

To share sync functionality, my best guess is to make a static Synchronizer class and pass it Syncable model objects. Synchronizer methods will use the Syncable interface to operate directly on model objects' shared, sync-related fields and delegate operations on type-specific fields. Alternatively, I could provide each model object its own Synchronizer instance...

Trying to find the right way to work around the inheritance limitation is really stretching my OOP skills... help is appreciated!

like image 977
Nolan Amy Avatar asked Apr 28 '26 18:04

Nolan Amy


1 Answers

I had the same issue when I found out that realmObjects should inherit directly form RealmObject class (no support for inheritance). In order to get back the benefits of polymorphism, I considered a similar solution to yours combined with some composition tricks that would avoid me attribute duplication.

"Talk is cheap show me the code."

Code examples

interface IPerson {
    String getName();
}

class Person extends RealmObject implements IPerson {

    String name;

    @Override
    public String getName() {
        return name;
    }
}

interface IWorker extends IPerson {
    int getSalary();
}

class Worker extends RealmObject implements IWorker {

    Person person;
    int salary;

    @Override
    public String getName() {
        return person.getName();
    }

    @Override
    public int getSalary() {
        return salary;
    }
}

Some benefits

You won't have to duplicate your attributes in each extending class.

Polymorphism is back! For example, now you can simulate a cast (with getPerson() in this example).

Some limits

When using a serialization library that uses reflection (suppose it's Gson), your serialized models will have their parents attributes embedded. Not something that you would have had if you were using classic inheritance.

Example with JSON

Let's suppose John Doe is making 500$ a month. (He's a Worker and a Person right?).

With classic inheritance, John Doe would look like this:

{
  "name":"John Doe",
  "salary": 500
}

But with this inheritance workaround ... :

{
  "person" : {
  "name":"John Doe"
  },
  "salary": 500
}

Hope this helps!

Note

PrimaryKeys unfortunately have to be duplicated.

Bonus

You might want to check RealmFieldNamesHelper, a library made by Christian Melchior "to make Realm queries more type safe".

like image 66
Anis LOUNIS Avatar answered May 01 '26 07:05

Anis LOUNIS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!