Realm Android:io.realm.exceptions.RealmException:' DomainModel'当前架构中不存在

时间:2017-09-06 08:44:28

标签: android realm

我在android应用程序中使用了realm db我按照文档集成了realm db,如下所示:

build.gradle(项目级别)

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

build.gradle(应用级别)

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'

以下是MyApplication Class中的代码

Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(Realm.DEFAULT_REALM_NAME)
                .schemaVersion(1)
                .deleteRealmIfMigrationNeeded()
                .build();

        Realm.getInstance(config);
        Realm.setDefaultConfiguration(config);

模型类

public class MyModel extends RealmObject {

    @PrimaryKey
    @SerializedName("message")
    private String message;

    @SerializedName("session_expired")
    private Integer sessionExpired;

    @SerializedName("domain_name")
    private String domainName;

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getSessionExpired() {
        return sessionExpired;
    }

    public void setSessionExpired(Integer sessionExpired) {
        this.sessionExpired = sessionExpired;
    }
}

现在,就调试版本而言,app正在按预期运行而不会发生任何崩溃。 但是当我生成发布版本时,应用程序会崩溃

  

io.realm.exceptions.RealmException:' MyModel'目前还不存在   架构。

请看一下上面的代码,请帮我解决这个问题。在此先感谢。

注意:我正在访问一个领域对象

  

Realm realm = Realm.getDefaultInstance();

以下是stacktrace

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   io.realm.exceptions.RealmException: 'MyModel' doesn't exist in current schema.
                                                                       at and.c(SourceFile:5112)
                                                                       at ans.a(SourceFile:48)
                                                                       at aon.a(SourceFile:68)
                                                                       at aop.d(SourceFile:285)
                                                                       at aop.a(SourceFile:178)
                                                                       at anb.a(SourceFile:3261)
                                                                       at abh.c(SourceFile:259)
                                                                       at com.mpose.com.mpose.activity.LoginActivity.k(SourceFile:306)
                                                                       at abh.b(SourceFile:1219)
                                                                       at com.mpose.com.mpose.activity.LoginActivity.loginClick(SourceFile:134)
                                                                       at com.mpose.com.mpose.activity.LoginActivity$$ViewBinder$1.doClick(SourceFile:18)
                                                                       at butterknife.internal.DebouncingOnClickListener.onClick(SourceFile:22)
                                                                       at android.view.View.performClick(View.java:4084)
                                                                       at android.view.View$PerformClick.run(View.java:16966)
                                                                       at android.os.Handler.handleCallback(Handler.java:615)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                       at android.os.Looper.loop(Looper.java:137)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                       at dalvik.system.NativeStart.main(Native Method)

以下是我访问MyModel类的方法

final RealmQuery<MyModel> query = mRealm.where(MyModel.class).equalTo("message", "Success");
        RealmResults<MyModel> result = query.findAll();

        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                MyModel myModel = query.findFirst();
                if (myModel != null) {
                    myModel.setDomainName(strDomain);
                }
            }
        });

1 个答案:

答案 0 :(得分:1)

您是否尝试过更新到最新版本? 定期检查the Realm changelog是否有错误修正是个好主意。

我遇到了类似的问题,并且更新到Realm v3.7.2解决了它。在更改日志中,您可以找到:

  

当前架构中不存在“'xxx'崩溃。”启用ProGuard时(#5211)。