Realm模型实际上需要吸气剂和吸气剂吗? [英] Does Realm models actually require getters and setters?

查看:74
本文介绍了Realm模型实际上需要吸气剂和吸气剂吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Realm模型中的字段实际上确实需要使用getter和setter方法,那么我找不到任何地方有明确记录的文档.例如,位于 https://realm.io/docs的文档/java/latest/api/io/realm/RealmObject.html

I cannot find it clearly documented anywhere if getters and setters are actually required for fields in a Realm Model. For example, the documentation at https://realm.io/docs/java/latest/api/io/realm/RealmObject.html says

RealmObject的唯一限制是不允许使用字段 是最终的,短暂的或易变的.任何方法以及公开 字段是允许的.提供自定义构造函数时, 没有参数的构造函数必须声明为空.

The only restriction a RealmObject has is that fields are not allowed to be final, transient' or volatile. Any method as well as public fields are allowed. When providing custom constructors, a public constructor with no arguments must be declared and be empty.

带有忽略"注释的字段没有这些限制,也没有 需要使用吸气剂或吸气剂.

Fields annotated with Ignore don't have these restrictions and don't require either a getter or setter.

这似乎暗示对于非忽略字段,getter和setter要求使用它.但是,该文档位于 https://realm.io/docs/java/latest/#定制对象

Which seems to hint that it is required with getters and setters for non-ignored fields. Yet, the documentation at https://realm.io/docs/java/latest/#customizing-objects says

几乎可以像POJO一样使用RealmObjects.从...延伸 RealmObject,您可以让字段公开,并使用简单 分配而不是setter和getter.

It is possible to use RealmObjects almost like POJOs. Extending from RealmObject, you can let the fields be public, and use simple assignments instead of setters and getter.

,然后显示一个领域模型的代码,该模型没有任何getter和setter,而是具有我们应该使用的公共字段.真的吗?我以为Realm甚至没有在实际字段中存储任何值,因此从它们读取和写入可能是个坏主意?我的意思是他们的调试文档 https://realm.io/docs/java/latest/#debugging 状态:

and then show the code for a Realm Model that does not have any getters and setters and instead have public fields we should use. Really? I thought Realm didn't even store any values in the actual fields, so reading and writing from them is probably a bad idea? I mean their debugging docs https://realm.io/docs/java/latest/#debugging state:

不幸的是,这些值是错误的,因为字段值不是 用过的. Realm在幕后创建一个代理对象,并覆盖 getter和setter以便访问领域中的持久数据

Unfortunately these values are wrong because the field values are not used. Realm creates a proxy object behind the scenes and overrides the getters and setters in order to access the persisted data in the Realm

那么有人可以启发我吗?我可以跳过获取方法和设置方法,而只坚持公共领域吗?是否有任何详尽的文档?

So could someone please enlighten me? Can I skip getters and setters and just stick with public fields? Is there any thorough docs on this?

推荐答案

public字段在大多数情况下都有效,并且自Realm 2.0.0起,它们甚至在RealmObjects的构造函数中也可以工作(允许默认值"),并且在您可以直接访问该属性.

public fields work in most cases, and since Realm 2.0.0 they work even in constructors of RealmObjects (allowing "default values"), and work if you directly access the property.

例如,

SomeObject obj = results.get(i);
obj.blah = "Blahblah";

之所以可行,是因为RealmObjects的托管字段访问已由Realm-Transformer转换为代理getter/setter调用(在本例中为realmSet$blah方法).

That works, because managed RealmObjects' field access are transformed by the Realm-Transformer into proxy getter/setter calls (in this case, into the realmSet$blah method).

自0.88.0开始,Realm开始作为Gradle插件提供时就是这种情况.

This has been the case since 0.88.0, when Realm started being provided as a Gradle plugin.

但是,主要的限制

However, a major limitation is that the proxy field access doesn't run in instrumentation tests, because the androidTestCompile scope does not run the transformer.

这篇关于Realm模型实际上需要吸气剂和吸气剂吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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