在 Android 上共享 Realm 字段 [英] Sharing Realm fields on Android

查看:45
本文介绍了在 Android 上共享 Realm 字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 上的 Realm 不支持模型继承/多态.p>

那么有没有办法在 Android 上共享字段?我们有 5 个模型,它们都共享相同的同步相关字段和代码.我们在当前的 SQLite 模型中使用继承;如果我们切换到 Realm,我们唯一的选择是在 5 个模型类中复制同步字段吗?

作为一种解决方法,我正在考虑让这些类实现一个 Syncable 接口,其中包含用于共享字段的 getter 和 setter,这至少让我可以共享同步功能.有没有更好的办法?

要共享同步功能,我最好的猜测是制作一个静态 Synchronizer 类并将其传递给 Syncable 模型对象.Synchronizer 方法将使用 Syncable 接口直接对模型对象的共享、同步相关字段进行操作,并委托对特定类型字段的操作.或者,我可以为每个模型对象提供它自己的 Synchronizer 实例...

试图找到解决继承限制的正确方法确实在扩展我的 OOP 技能...感谢您的帮助!

解决方案

当我发现 realmObjects 应该直接继承 RealmObject 类(没有支持继承).为了恢复多态性的好处,我考虑了与您的解决方案类似的解决方案,并结合了一些可以避免属性重复的组合技巧.

<块引用>

说话很便宜,给我看代码."

代码示例

interface IPerson {字符串 getName();}类 Person 扩展 RealmObject 实现 IPerson {字符串名称;@覆盖公共字符串 getName() {返回名称;}}接口 IWorker 扩展 IPerson {int getSalary();}类 Worker 扩展 RealmObject 实现 IWorker {人 人;国际工资;@覆盖公共字符串 getName() {返回人.getName();}@覆盖公共 int getSalary() {返还工资;}}

一些好处

您不必在每个扩展类中复制您的属性.

多态又回来了!例如,现在您可以模拟演员阵容(在本例中使用 getPerson()).

一些限制

当使用使用反射的序列化库(假设它是 Gson)时,您的序列化模型将嵌入其父属性.如果您使用经典继承,您将不会拥有这样的东西.

JSON 示例

假设 John Doe 每月赚 500 美元.(他既是工人又是人,对吧?).

使用经典继承,John Doe 看起来像这样:

<代码>{"name":"John Doe",工资":500}

但是使用这种继承解决方法...:

<代码>{人" : {名称":约翰·多伊"},工资":500}

希望这会有所帮助!

注意

很遗憾,必须复制主键.

奖金

您可能需要查看 RealmFieldNamesHelper,这是一个由 Christian Melchior让 Realm 查询更安全".

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!

解决方案

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

这篇关于在 Android 上共享 Realm 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆