本文介绍了新 RealmList 中 String 字段的 Realm 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Conversation 类以前有一个名为message"的字符串字段.现在这个message"字段被删除了,我在 Conversation 类中添加了一个RealmList allMessages"而不是旧的message"字段.ChatMessage 类有一个字段名称messageText".如何将消息"从对话类迁移到列表allMessages"的messageText"?

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;

    ....
}

旧会话课.

public class Conversation extends RealmObject implements Parcelable{

    private String message;
    ....
}

新建会话类.

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

推荐答案

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 中 String 字段的 Realm 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:52