此Realm的架构中缺少该类

时间:2017-08-09 06:42:13

标签: java android realm

项目设置

  • Android studio 2.3.3
  • Realm 3.5.0

来源

RealmController.java

public RealmController(Context context) {
    try {
        this.context = context;
        Realm.init(context);
        PrintLog("RealmController", "RealmController", "Init realm", LOG_LEVEL_INFO);
        realmInstance = Realm.getDefaultInstance();
        PrintLog("RealmController", "RealmController", "Getting realm instance", LOG_LEVEL_INFO);
    }
    catch (Exception err) {
        PrintLog("RealmController", "RealmController", "Error: " + err.getMessage(), LOG_LEVEL_ERROR);
    }
}

SellingData.java

public class SellingDataTable extends RealmObject {

    public Date todaysDate;
    public int sellingData;

    public Date getTodaysDate() {
        return todaysDate;
    }
    public void setTodaysDate(Date todaysDate) {
        this.todaysDate = todaysDate;
    }
    public int getSellingData() {
        return sellingData;
    }
    public void setSellingData(int sellingData) {
        this.sellingData = sellingData;
    }
}

预期行为

  • 获取领域默认实例。

实际行为

会崩溃。

08-09 15:24:16.044 [I2maxMain] {Init} (preparing ui)
08-09 15:24:16.120 [RealmController] {RealmController} (Init realm)
08-09 15:24:16.129 [RealmController] {RealmController} (Error: The 'SellingDataTable' class is missing from the schema for this Realm.)

2 个答案:

答案 0 :(得分:3)

如果您在设备上创建具有给定架构的Realm,那么如果您开始修改架构(通过添加新类,添加新字段,删除字段,添加/删除@Index,添加/删除{{ 1}},更改类型等)然后您需要提供迁移(例如here),或者您需要在RealmConfiguration上指定@Required

答案 1 :(得分:1)

如果您添加了新的模型类。您需要卸载该应用并再次运行。它将解决问题。