"realm migration needed",从领域数据库中检索值时 android 中的异常
"realm migration needed", exception in android while retrieving values from realm db
我在我的应用程序中使用 Realm 作为后端。我创建了一个名为 table 的设置。我按照 Realm 官方网站上给出的步骤在 table 中添加了值。
但是当我要从 table 中检索值时,出现异常
"io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided" on the line:" realm=Realm.getInstance(getApplicationContext());".
其实我是android 和Realm 的新手,所以找麻烦了解问题所在。
您更改了领域结构。
为了修复它,您应该包括 migration
或者干脆删除应用程序并重新安装。
如果您将应用程序上传到商店,"delete and reinstall the app" 将不会对其他用户起作用,因此您必须使用 "deleting" 领域和 "reinstalling" 领域,而不是应用程序.方法如下,希望对你有帮助!!
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
try {
return Realm.getInstance(realmConfiguration);
} catch (RealmMigrationNeededException e){
try {
Realm.deleteRealm(realmConfiguration);
//Realm file has been deleted.
return Realm.getInstance(realmConfiguration);
} catch (Exception ex){
throw ex;
//No Realm file to remove.
}
}
编辑
对于最新的Realm (3.0.0),Realm改变了构造函数结构,所以你必须这样做:
Realm.init(context);
RealmConfiguration config = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
EDIT: for new versions of Realm, Realm.init(Context context) is added
Realm.init(context);
RealmConfiguration config = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
注意:使用此配置选项,任何架构更改都将导致 数据丢失,具体而言:
- 一个字段是added/removed
- 添加了一个新的 RealmObject class
- 删除了现有的 RealmObject
@Required
是 added/removed
@PrimaryKey
是 added/removed
@Index
是 added/removed
因此主要推荐在应用处于开发阶段时使用。
或者按照官方文档添加迁移:
https://realm.io/docs/java/latest/#migrations
例如,
public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
RealmObjectSchema personSchema = schema.get("Person");
personSchema
.addField("fullName", String.class, FieldAttribute.REQUIRED);
oldVersion++;
...
// hash code, equals
和
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder()
.migration(new Migration())
// .deleteRealmIfMigrationNeeded()
.build();
对我有用
Realm.init(context);
Realm realm;
try{
realm = Realm.getDefaultInstance();
}catch (Exception e){
// Get a Realm instance for this thread
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(config);
}
Kotlin 版本:
val realm = try {
Realm.init(this)
val config = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
Realm.getInstance(config)
} catch (ex: RealmMigrationNeededException) {
Realm.getDefaultInstance()
}
我在我的应用程序中使用 Realm 作为后端。我创建了一个名为 table 的设置。我按照 Realm 官方网站上给出的步骤在 table 中添加了值。 但是当我要从 table 中检索值时,出现异常
"io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided" on the line:" realm=Realm.getInstance(getApplicationContext());".
其实我是android 和Realm 的新手,所以找麻烦了解问题所在。
您更改了领域结构。
为了修复它,您应该包括 migration 或者干脆删除应用程序并重新安装。
如果您将应用程序上传到商店,"delete and reinstall the app" 将不会对其他用户起作用,因此您必须使用 "deleting" 领域和 "reinstalling" 领域,而不是应用程序.方法如下,希望对你有帮助!!
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
try {
return Realm.getInstance(realmConfiguration);
} catch (RealmMigrationNeededException e){
try {
Realm.deleteRealm(realmConfiguration);
//Realm file has been deleted.
return Realm.getInstance(realmConfiguration);
} catch (Exception ex){
throw ex;
//No Realm file to remove.
}
}
编辑
对于最新的Realm (3.0.0),Realm改变了构造函数结构,所以你必须这样做:
Realm.init(context);
RealmConfiguration config = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
EDIT: for new versions of Realm, Realm.init(Context context) is added
Realm.init(context);
RealmConfiguration config = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
注意:使用此配置选项,任何架构更改都将导致 数据丢失,具体而言:
- 一个字段是added/removed
- 添加了一个新的 RealmObject class
- 删除了现有的 RealmObject
@Required
是 added/removed@PrimaryKey
是 added/removed@Index
是 added/removed
因此主要推荐在应用处于开发阶段时使用。
或者按照官方文档添加迁移:
https://realm.io/docs/java/latest/#migrations
例如,
public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
RealmObjectSchema personSchema = schema.get("Person");
personSchema
.addField("fullName", String.class, FieldAttribute.REQUIRED);
oldVersion++;
...
// hash code, equals
和
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder()
.migration(new Migration())
// .deleteRealmIfMigrationNeeded()
.build();
对我有用
Realm.init(context);
Realm realm;
try{
realm = Realm.getDefaultInstance();
}catch (Exception e){
// Get a Realm instance for this thread
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(config);
}
Kotlin 版本:
val realm = try {
Realm.init(this)
val config = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
Realm.getInstance(config)
} catch (ex: RealmMigrationNeededException) {
Realm.getDefaultInstance()
}