无法从领域对象检索字段值,调试器中的值为null [英] Cannot retrieve field values from realm object, values are null in debugger

查看:91
本文介绍了无法从领域对象检索字段值,调试器中的值为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RealmProxy类似乎隐藏了我的RealmObject值,但可以从proxyclass设置它.

It seems like my RealmObject values are being hidden by the RealmProxy class, but can be set from the proxyclass.

如您所见,我的模型很简单.

My model is pretty straight forward as you can see.

public class GroupRealm extends RealmObject {
    @PrimaryKey
    public String id;
    @Index
    public String name;
    public String imageUrl;
    public int order;

    public GroupRealm parent;
    public RealmList<GroupRealm> children;
    public RealmList<ContentRealm> contents;
}

这是我设置值的方式(db是有效的Realm,并且一切都在提交正常的事务中):

This is how i am setting the values(db is a valid Realm, and everything is in a transaction that commits fine):

GroupRealm gr = db.where(GroupRealm.class).equalTo("id",g.GroupID).findFirst();
        if(gr==null){
            gr = db.createObject(GroupRealm.class,g.GroupID);
        }
        gr.imageUrl = g.GlyphUrl;
        gr.name = g.Title;
        gr.order = g.OrderNum;

下面的图像是我查询后面的数据库时得到的.(相同的变量名在代码中的位置不同)

The image below is what I get when i query the db latter on.(same variable name not same place in code)

在我的RealmObjects定义项目的android.library中,我具有必要的插件.

In my android.library where my RealmObjects are defined project I have the necessary plugins.

apply plugin: 'com.android.library'
apply plugin: 'realm-android'

在项目级别,我正在设置正确的依赖项:

and on the project level I am setting the correct dependencies:

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0'
    classpath "io.realm:realm-gradle-plugin:0.90.1"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

我没主意了.如果我尝试访问任何内容,都将按预期方式检索GroupRealm,但是通过代理类公开的所有公共属性都将返回null!

I am out of ideas. If I try to access anything I retrieve the GroupRealm as expected but all of the public properties exposed through the proxy class return null!

推荐答案

文档中的相关常见问题解答: https://realm.io/docs/java/latest/#debugging

Relevant FAQ in documentation: https://realm.io/docs/java/latest/#debugging

Realm使用Android Gradle Transform API.在将已编译的类文件转换为dex文件之前,它有可能对其进行处理.
io.realm.transformer.RealmTransformer io.realm.transformer中的更多详细信息.可以在领域的github中找到的BytecodeModifier 类.

Realm uses Android Gradle Transform API. It gives a possibility to manipulate compiled class files before they are converted to dex files.
More details inside io.realm.transformer.RealmTransformer and io.realm.transformer. BytecodeModifier classes which can be found in the realm's github.

RealmTransformer的主要功能是:

What RealmTransformer does, among others, is:

  • 使用适当的Realm访问器替换对用户RealmObjects字段的所有访问.

您还可以在文件夹 app/build/intermediates/transforms/RealmTransformer/

二传手的例子:
您的代码行:

Example of setter:
Line of your code:

gr.imageUrl = g.GlyphUrl;

将替换为以下内容:

String var5 = g.GlyphUrl;
gr.realmSet$imageUrl(var5);

吸气剂示例:

String url = gr.imageUrl;

将替换为以下内容:

String url = gr.realmGet$imageUrl();

用例示例

  1. 您已创建类 GroupRealm .使用Transform API的领域会生成 GroupRealmRealmProxy .该代理类如下所示:

  1. You have created class GroupRealm. Realm using Transform API generates GroupRealmRealmProxy. This proxy class looks like this:

public class GroupRealmRealmProxy extends GroupRealm implements RealmObjectProxy, GroupRealmRealmProxyInterface {
    private final GroupRealmRealmProxy.GroupRealmColumnInfo columnInfo;
    private final ProxyState proxyState;
    private RealmList<GroupRealm> childrenRealmList;
    private RealmList<ContentRealm> contentsRealmList;
    private static final List<String> FIELD_NAMES;

    GroupRealmRealmProxy(ColumnInfo columnInfo) {
        ...
    }

    public String realmGet$id() {
        this.proxyState.getRealm$realm().checkIfValid();
        return this.proxyState.getRow$realm().getString(this.columnInfo.idIndex);
    }

    public void realmSet$id(String value) {
        this.proxyState.getRealm$realm().checkIfValid();
        if(value == null) {
            this.proxyState.getRow$realm().setNull(this.columnInfo.idIndex);
        } else {
           this.proxyState.getRow$realm().setString(this.columnInfo.idIndex, value);
        }
    }

    public String realmGet$name() {
        this.proxyState.getRealm$realm().checkIfValid();
        return this.proxyState.getRow$realm().getString(this.columnInfo.nameIndex);
    }

    public void realmSet$name(String value) {
        this.proxyState.getRealm$realm().checkIfValid();
        if(value == null) {
            this.proxyState.getRow$realm().setNull(this.columnInfo.nameIndex);
        } else {
            this.proxyState.getRow$realm().setString(this.columnInfo.nameIndex, value);
        }
     }

    ...
}

您会发现,方法 realmSet $ name realmGet $ name 无法访问在GroupRealm类中声明的字段 name .他们使用 proxyState .

You can observe that methods realmSet$name and realmGet$name don't have access to field name declared in the class GroupRealm. They use proxyState.

现在,让我们回到GroupRealm的用法.当您调试代码时:

Now, let's back to the usage of GroupRealm. When you debug your code:

GroupRealm gr = db.where(GroupRealm.class).equalTo("id",g.GroupID).findFirst();
if(gr==null){
    gr = db.createObject(GroupRealm.class,g.GroupID);
}
gr.imageUrl = g.GlyphUrl;
gr.name = g.Title;
gr.order = g.OrderNum;

实际上,它的反编译版本是这样的:

in a reality it's decompiled version looks like this:

GroupRealm gr = (GroupRealm)realm.where(GroupRealm.class).equalTo("id", g.GroupId).findFirst();
if(gr == null) {
    gr = (GroupRealm)realm.createObject(GroupRealm.class, g.GroupId);
}

String var7 = g.GlyphUrl;
gr.realmSet$imageUrl(var7);
var7 = g.Title;
gr.realmSet$name(var7);
int var8 = g.OrderNum;
gr.realmSet$order(var8);

首先, gr GroupRealmRealmProxy 类的实例.如您所见,gr.name的设置被替换为 gr.realmSet $ name(var7).这表示从未使用 GroupRealm 的字段 name .对于 realmGet $ .

First of all, gr is the instance of GroupRealmRealmProxy class. As you can see, setting of gr.name is replaced by gr.realmSet$name(var7). It means that the field name of GroupRealm is never used. The situation is analogous in the case of realmGet$.

在调试时,您会看到源代码的版本,但实际上您使用的是带有注入方法 realmSet $ realmGet $ 的修改版本.

While debugging you see your version of source code but actually you're using a modified version with injected methods realmSet$ and realmGet$.

这篇关于无法从领域对象检索字段值,调试器中的值为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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