The class is missing from the schema for this Realm

左心房为你撑大大i 提交于 2019-12-31 02:46:06

问题


Project setting

  • Android studio 2.3.3
  • Realm 3.5.0

Source

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;
    }
}

Expected behavior

  • Get realm default instance.

Actual behavior

Gonna crash.

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.)

回答1:


If you create a Realm with a given schema on the device, then if you start modifying the schema (by adding new classes, adding new fields, removing fields, adding/removing @Index, adding/removing @Required, changing a type, etc.) then you either need to provide a migration (example here), or you need to specify deleteIfMigrationNeeded() on your RealmConfiguration.




回答2:


If you have added a new model class. You need to uninstall the app and run again. It will solve the problem.



来源:https://stackoverflow.com/questions/45583336/the-class-is-missing-from-the-schema-for-this-realm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!