领域IllegalStateException:模式验证失败

时间:2018-07-06 07:22:21

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

我正在尝试在我的项目中使用领域数据库
这是我的代码段

在应用程序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;
}

 }

在App启动时尝试调用此函数。我有一个例外情况

 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.

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

1 个答案:

答案 0 :(得分:0)

除非使用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 {
}