Sharing Realm fields on Android

后端 未结 2 486
日久生厌
日久生厌 2020-12-21 15:42

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 synchroniz

相关标签:
2条回答
  • 2020-12-21 16:32

    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".

    0 讨论(0)
  • 2020-12-21 16:40

    If you use Kotlin, sharing the fields via an interface becomes even more trivial:

    interface PersonBase {
        var name: String?
        var salary: Int
    }
    

    Then

    class Person: RealmObject(), PersonBase {
    }
    
    0 讨论(0)
提交回复
热议问题