【发布时间】:2025-12-21 21:00:09
【问题描述】:
好的。所以我从android中完全删除了我的应用程序。然后在全新安装时出现错误
Field already exists in 'PortfolioCoin': color.
为什么领域尝试在全新安装时迁移?
我在我的应用程序文件中找到了这个
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(1)
.migration(new Migration())
//.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(configuration);
Realm.compactRealm(configuration);
这是我的迁移文件
public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
// During a migration, a DynamicRealm is exposed. A DynamicRealm is an untyped variant of a normal Realm, but
// with the same object creation and query capabilities.
// A DynamicRealm uses Strings instead of Class references because the Classes might not even exist or have been
// renamed.
// Access the Realm schema in order to create, modify or delete classes and their fields.
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
RealmObjectSchema portfolioCoinSchema = schema.get("PortfolioCoin");
portfolioCoinSchema
.addField("color", int.class)
.addField("totalValueBTC", double.class);
oldVersion++;
}
}
}
【问题讨论】:
-
您找到解决方案了吗?我遇到了同样的问题,但崩溃是在以前的迁移版本中。在我当前的应用中,迁移版本是 10,但崩溃发生在
if (oldVersion == 6)。 -
我在小米 Redmi Note 设备中遇到了这个问题。