Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

java - Transform an object to a list[object] in Realm as migration ( How can i replace a list[objects] with object field in Realm? )

I want to import existing token into tokenList as first item , for example if someone has a token="abc" after migration , i want he/she has tokenList that tokenList[0]=="abc"

This is my migration code:

public class RealmMigration implements io.realm.RealmMigration {
.
.
.
    if (oldVersion == 3) {
        final RealmObjectSchema bankSchema = realmSchema.get("Bank");
        final RealmObjectSchema tokenSchema = realmSchema.get("Token");
        DynamicRealmObject bank = realm.createObject("Bank",1);
        Token token = bank.get("Token");
        bankSchema.addRealmListField("tokenList", tokenSchema);
        List<Token> tokenList = bank.get("tokenList");
        if (token != null) tokenList.add(token);
        bankSchema.removeField("Token");
    }
}

I replace this in new version :

@RealmField(name = "tokenList")
private RealmList<Token> mTokenList;

With this in old version in my class :

@RealmField(name = "Token")
private Token mToken;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I created new issue in Realm GitHub then i found right way for transform object to list[object] in Realm and insert existing object from previous version to new version of application.

In this example we want to transform Token object to tokenList[Token] in Bank model.

In last version of migration put this :

public class RealmMigration implements io.realm.RealmMigration {

.

.

.

        if (oldVersion == 3) {
            //Create a schema for Bank
            final RealmObjectSchema bankSchema = realmSchema.get("Bank");
            //Create a schema for Token that has been in Bank model
            final RealmObjectSchema tokenSchema = realmSchema.get("Token");
            //We use bankSchema for add tokenList field and transform
            bankSchema
                .addRealmListField("tokenList", tokenSchema)
                .transform((obj) -> {//obj is bank model,we have a transform in bank model
                         DynamicRealmObject token = obj.get("Token");
                         List<DynamicRealmObject> tokenList = obj.get("tokenList");
                                //We add token from pre version to tokenList in new version
                                if (token != null) tokenList.add(token);
                            }
                    )
                    //Finally remove unwanted field
                    .removeField("Token");
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...