我正在Android应用中使用Realm,我想执行以下操作:
我为上述更改编写了以下迁移:
public class MyMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
RealmObjectSchema objectSchema = schema.get("AccessInfo");
objectSchema.addIndex("bookId");
objectSchema.addPrimaryKey("bookId");
objectSchema.removeField("lastOpenedTimeStamp");
objectSchema.addField("lastOpenedTimeStamp", String.class);
oldVersion++;
}
}
但是我仍然在MainActivity中遇到以下错误:
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'AccessInfo.bookId' has been made indexed.
- Property 'AccessInfo.lastOpenedTimeStamp' has been changed from 'int' to 'string'.
- Primary Key for class 'AccessInfo' has been added.
以下是我的领域配置
defaultConfig = new RealmConfiguration.Builder().modules(new DefaultModule()).migration(new MyMigration()).build();
realm = Realm.getInstance(defaultConfig);
AccessInfo:
public class AccessInfo extends RealmObject {
@PrimaryKey
int bookId;
String lastOpenedTimeStamp;
int lastOpenedPageId;
int lastOpenedPageNumber;
int lastOpenedPartNumber;
int accessCount;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getLastOpenedTimeStamp() {
return lastOpenedTimeStamp;
}
public void setLastOpenedTimeStamp(String lastOpenedTimeStamp) {
this.lastOpenedTimeStamp = lastOpenedTimeStamp;
}
public int getLastOpenedPageId() {
return lastOpenedPageId;
}
public void setLastOpenedPageId(int lastOpenedPageId) {
this.lastOpenedPageId = lastOpenedPageId;
}
public int getLastOpenedPageNumber() {
return lastOpenedPageNumber;
}
public void setLastOpenedPageNumber(int lastOpenedPageNumber) {
this.lastOpenedPageNumber = lastOpenedPageNumber;
}
public int getLastOpenedPartNumber() {
return lastOpenedPartNumber;
}
public void setLastOpenedPartNumber(int lastOpenedPartNumber) {
this.lastOpenedPartNumber = lastOpenedPartNumber;
}
public int getAccessCount() {
return accessCount;
}
public void setAccessCount(int accessCount) {
this.accessCount = accessCount;
}
}
感谢任何支持。 谢谢。