Realm Migration of a String field inside a new RealmList

旧街凉风 提交于 2021-01-04 07:05:31

问题


Conversation class previously had a string field named "message". Now this "message" field is removed and i have added a "RealmList allMessages" inside Conversation class instead of old "message" field. ChatMessage class has a field name "messageText". How can i migrate "message" from Conversation class to "messageText" of list "allMessages"??

public class ChatMessage extends RealmObject implements Parcelable {

    ....

    private String messageText;

    ....
}

Old Conversation class.

public class Conversation extends RealmObject implements Parcelable{

    private String message;
    ....
}

New Conversation class.

public class Conversation extends RealmObject implements Parcelable{

     private RealmList<ChatMessage> allMessages;
}

I tried till this. But it is not working.

        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");
        }
    }

回答1:


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");
        }



回答2:


my use-case was a bit different. i had a RealmList<RealmString> which i wanted to migrate to RealmList<String>

here's my migration:

// 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++;
}


来源:https://stackoverflow.com/questions/51366887/realm-migration-of-a-string-field-inside-a-new-realmlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!