这是我的领域配置代码。
问题是,当我删除或添加任何PrimaryKey
时,我收到RealmMigrationNeededException
例外。
previsoly UpgradeInfo.class,RealmApplication.class
public class UpgradeInfo extends RealmObject{
@SerializedName("version")
@PrimaryKey
private int versionCode;
@SerializedName("version_name")
private String versionName;
}
public class RealmApplication extends MultiDexApplication {
@Override
public void onCreate() {
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(1)
.build();
Realm.setDefaultConfiguration(config);
}
}
新的UpgradeInfo.class,RealmApplication.class
public class UpgradeInfo extends RealmObject{
@PrimaryKey
private String packageName
@SerializedName("version")
private int versionCode;
@SerializedName("version_name")
private String versionName;
}
public class RealmApplication extends MultiDexApplication {
@Override
public void onCreate() {
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(2)
.migration(new Migration())
.build();
Realm.setDefaultConfiguration(config);
}
}
public class ZNJRealmMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 1) {
schema.get("UpgradeInfo").removePrimaryKey().addField("packageName", String.class).addPrimaryKey("packageName");
}
}
}
我收到此错误:
java.lang.RuntimeException: Unable to stop activity {com.example.realm/com.example.realm.main.ui.MainActivity}:
io.realm.exceptions.RealmMigrationNeededException:
Migration is required due to the following errors:
- Primary Key for class 'UpgradeInfo' has been added.
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3668)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3722)
at android.app.ActivityThread.access$1100(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1522)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5682)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Primary Key for class 'UpgradeInfo' has been added.
at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:184)
at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:254)
at io.realm.BaseRealm.<init>(BaseRealm.java:124)
at io.realm.BaseRealm.<init>(BaseRealm.java:93)
at io.realm.Realm.<init>(Realm.java:153)
at io.realm.Realm.createInstance(Realm.java:424)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
at io.realm.Realm.getDefaultInstance(Realm.java:332)
at com.example.realm.db.RealmHelper.<init>(RealmHelper.java:35)
at com.example.realm.db.RealmHelper.getInstance(RealmHelper.java:45)
at com.example.realm.util.Util.closeRealm(Util.java:181)
at com.example.realm.main.ui.MainActivity.onStop(MainActivity.java:82)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1288)
at android.app.Activity.performStop(Activity.java:6328)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3665)
... 10 more
答案 0 :(得分:0)
创建没有架构版本的RealmConfiguration时,其架构版本设置为0
。
因此,您的代码很可能无法正常工作,因为:
您使用的是不具有迁移设置的其他配置
您的架构版本曾经是0而不是1,因此您实际上并没有进行会更改主键的架构操作