Does Realm models actually require getters and setters?

前端 未结 1 1425
小鲜肉
小鲜肉 2021-01-11 15:55

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/doc

相关标签:
1条回答
  • 2021-01-11 16:36

    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.

    For example,

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

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

    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.

    0 讨论(0)
提交回复
热议问题