【问题标题】:Realm IllegalStateException: Schema validation failed领域 IllegalStateException:架构验证失败
【发布时间】:2018-07-06 07:22:20
【问题描述】:

我正在尝试在我的项目中使用 Realm 数据库
这是我的代码sn-ps

在应用程序java代码中

public class CoreApplication extends Application {
private static CoreApplication sInstance;
public static final String F_PREFERENCE = "K_PREFERENCES";
public static Realm realm;

@Override
public void onCreate() {
    super.onCreate();
    sInstance = this;
    LocaleManager.setLocale(this);

    realm.init(this);
    realm = Realm.getDefaultInstance();
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
            .name("mydb.realm")
            .schemaVersion(2)
            .build();
    Realm.setDefaultConfiguration(realmConfiguration);

}

@Override
public void onTerminate() {
    Realm.getDefaultInstance().close();
    super.onTerminate();
}

}

RealmObject java 类

public class CashierTable extends RealmObject implements Serializable {
@Index
@PrimaryKey
private long id;
private String name = "";
private String password = "";
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

 }

当我在应用程序启动时尝试调用此函数时。我有一个异常

 public static CashierTable getSingleCashierTable() {
    CoreApplication.realm = Realm.getDefaultInstance();
    AtomicReference<CashierTable> cashierTable=new AtomicReference<>();
    CoreApplication.realm .executeTransaction(realm -> {
        cashierTable.set(realm.where(CashierTable.class).findFirst());
    });
    return cashierTable.get();
}

这是我的日志结果

java.lang.RuntimeException: Unable to create application com.unipay.posApp.CoreApplication: java.lang.IllegalStateException: Schema validation failed due to the following errors:
- Type 'CashierTable' appears more than once in the schema.

我结清了现金,但仍然有同样的问题。
谁能解释一下我的代码有什么问题?

【问题讨论】:

  • 在您的数据库架构中多次创建出纳表。尝试数据库的架构。 @BekaKK
  • 或者,仅出于测试和调试目的,您可以在应用程序级别创建配置时设置此标志deleteIfMigrationNeeded() 并检查它是否有效,但这会删除您的表,因此仅将其用于测试并调试错误。
  • 在项目的不同包中查找 CashierTable,如果有重复。
  • 感谢大家的关注。但是我的应用程序是第一次启动,我的目标是检查 cashierTable 是否为空或其他内容
  • 是的,你是对的。这是一个不同的包。是否有必要成为同一个包@Sahil

标签: android realm realm-mobile-platform realm-migration


【解决方案1】:

除非您使用 Realm 5.0+ 并使用 @RealmClass(name="... 为您的至少一个类指定不同的表名,否则您不能有两个具有相同类名的 RealmObject 类。

package io.package.first;

public class Dog extends RealmObject {
}

package io.package.second;

@RealmClass(name="SecondDog")
public class Dog extends RealmObject {
}

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2014-05-02
    相关资源
    最近更新 更多