新 RealmList 中字符串字段的领域迁移
Realm Migration of a String field inside a new RealmList
对话 class 之前有一个名为 "message" 的字符串字段。现在这个 "message" 字段被删除,我在 Conversation class 中添加了一个 "RealmList allMessages" 而不是旧的 "message" 字段。 ChatMessage class 有一个字段名称 "messageText"。我如何将 "message" 从对话 class 迁移到列表 "allMessages" 的 "messageText"?
public class ChatMessage extends RealmObject implements Parcelable {
....
private String messageText;
....
}
旧对话class。
public class Conversation extends RealmObject implements Parcelable{
private String message;
....
}
新对话class。
public class Conversation extends RealmObject implements Parcelable{
private RealmList<ChatMessage> allMessages;
}
我试过了。但它不起作用。
if (oldVersion == 6){
RealmObjectSchema messageSchema = schema.create("ChatMessage")
.addField("messageText", String.class);
RealmObjectSchema userChatSchema = schema.get("Conversation");
if (userChatSchema != null) {
userChatSchema.addRealmListField("message_tmp", messageSchema)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(@NonNull DynamicRealmObject obj) {
String message = obj.getString("message");
RealmList<DynamicRealmObject> valueObj = obj.getList("message_tmp");
//What next?Am i right till this point??
}
})
.removeField("message")
.renameField("message_tmp", "allMessages");
}
}
RealmObjectSchema messageSchema = schema.create("ChatMessage")
.addField("txt", String.class);
RealmObjectSchema userChatSchema = schema.get("Conversation");
if (userChatSchema != null) {
userChatSchema.addRealmListField("message_tmp", messageSchema)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(@NonNull DynamicRealmObject obj) {
String message = obj.getString("message");
RealmList<DynamicRealmObject> valueObj = obj.getList("message_tmp");
DynamicRealmObject dynamicRealmObject = realm.createObject("ChatMessage");
if (message != null)
dynamicRealmObject.set("txt", message);
else
dynamicRealmObject.set("txt", "This message has been deleted");
valueObj.add(dynamicRealmObject);
}
})
.removeField("message")
.renameField("message_tmp", "allMessages");
}
我的用例有点不同。我有一个 RealmList<RealmString>
,我想迁移到 RealmList<String>
这是我的迁移:
// delete all RealmString from the database because they should be replaced with String
// RealmString is used to create lists of strings
if (oldVersion == 56L) {
// convert RealmList<RealmString> into RealmList<String>
fun RealmObjectSchema.migrateToListOfStringPrimitives(fieldName: String) {
addRealmListField("${fieldName}_tmp", String::class.java)
transform { obj ->
val realmListOfRealmStrings = obj.getList(fieldName)
val realmListOfStrings = obj.getList("${fieldName}_tmp", String::class.java)
realmListOfRealmStrings.mapTo(realmListOfStrings) { it.getString("value") }
}
removeField(fieldName)
renameField("${fieldName}_tmp", fieldName)
}
schema[Assessment::class.java.simpleName]!!
.migrateToListOfStringPrimitives("bodyPartInjured")
schema[R2PProtocol::class.java.simpleName]!!
.migrateToListOfStringPrimitives("sports")
schema[NoteAssessment::class.java.simpleName]!!
.migrateToListOfStringPrimitives("images")
// delete all dangling RealmStings from DB
schema.remove("RealmString")
oldVersion++;
}
对话 class 之前有一个名为 "message" 的字符串字段。现在这个 "message" 字段被删除,我在 Conversation class 中添加了一个 "RealmList allMessages" 而不是旧的 "message" 字段。 ChatMessage class 有一个字段名称 "messageText"。我如何将 "message" 从对话 class 迁移到列表 "allMessages" 的 "messageText"?
public class ChatMessage extends RealmObject implements Parcelable {
....
private String messageText;
....
}
旧对话class。
public class Conversation extends RealmObject implements Parcelable{
private String message;
....
}
新对话class。
public class Conversation extends RealmObject implements Parcelable{
private RealmList<ChatMessage> allMessages;
}
我试过了。但它不起作用。
if (oldVersion == 6){
RealmObjectSchema messageSchema = schema.create("ChatMessage")
.addField("messageText", String.class);
RealmObjectSchema userChatSchema = schema.get("Conversation");
if (userChatSchema != null) {
userChatSchema.addRealmListField("message_tmp", messageSchema)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(@NonNull DynamicRealmObject obj) {
String message = obj.getString("message");
RealmList<DynamicRealmObject> valueObj = obj.getList("message_tmp");
//What next?Am i right till this point??
}
})
.removeField("message")
.renameField("message_tmp", "allMessages");
}
}
RealmObjectSchema messageSchema = schema.create("ChatMessage")
.addField("txt", String.class);
RealmObjectSchema userChatSchema = schema.get("Conversation");
if (userChatSchema != null) {
userChatSchema.addRealmListField("message_tmp", messageSchema)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(@NonNull DynamicRealmObject obj) {
String message = obj.getString("message");
RealmList<DynamicRealmObject> valueObj = obj.getList("message_tmp");
DynamicRealmObject dynamicRealmObject = realm.createObject("ChatMessage");
if (message != null)
dynamicRealmObject.set("txt", message);
else
dynamicRealmObject.set("txt", "This message has been deleted");
valueObj.add(dynamicRealmObject);
}
})
.removeField("message")
.renameField("message_tmp", "allMessages");
}
我的用例有点不同。我有一个 RealmList<RealmString>
,我想迁移到 RealmList<String>
这是我的迁移:
// delete all RealmString from the database because they should be replaced with String
// RealmString is used to create lists of strings
if (oldVersion == 56L) {
// convert RealmList<RealmString> into RealmList<String>
fun RealmObjectSchema.migrateToListOfStringPrimitives(fieldName: String) {
addRealmListField("${fieldName}_tmp", String::class.java)
transform { obj ->
val realmListOfRealmStrings = obj.getList(fieldName)
val realmListOfStrings = obj.getList("${fieldName}_tmp", String::class.java)
realmListOfRealmStrings.mapTo(realmListOfStrings) { it.getString("value") }
}
removeField(fieldName)
renameField("${fieldName}_tmp", fieldName)
}
schema[Assessment::class.java.simpleName]!!
.migrateToListOfStringPrimitives("bodyPartInjured")
schema[R2PProtocol::class.java.simpleName]!!
.migrateToListOfStringPrimitives("sports")
schema[NoteAssessment::class.java.simpleName]!!
.migrateToListOfStringPrimitives("images")
// delete all dangling RealmStings from DB
schema.remove("RealmString")
oldVersion++;
}