Android:更新领域表和迁移

时间:2016-03-24 07:41:05

标签: android realm

这是我目前的领域迁移配置

 RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                                            .schemaVersion(2)
                                            .migration(new Migration())
                                            .build();


public class Migration implements RealmMigration {

        @Override
        public void migrate(final DynamicRealm realm, long oldVersion, long newVersion){

            RealmSchema schema = realm.getSchema();

            if(oldVersion == 1){
                RealmObjectSchema sweatPointSchema = schema.get("UserInfoDB");
                        sweatPointSchema.addField("sweatPoints",Integer.class);

                oldVersion++;
            }

            if(oldVersion == 2){
                RealmObjectSchema teamInfoSchema = schema.get("TeamInfoDB");
                        teamInfoSchema.addField("teamDescription",String.class);

                oldVersion ++;
            }



        }
    }

现在我需要为下一次迁移更新一些特定的表所以我按照以下更新我的领域配置和迁移

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                                            .schemaVersion(3)
                                            .migration(new Migration())
                                            .build();

 public class Migration implements RealmMigration {

        @Override
        public void migrate(final DynamicRealm realm, long oldVersion, long newVersion){

            RealmSchema schema = realm.getSchema();

            if(oldVersion == 1){
                RealmObjectSchema sweatPointSchema = schema.get("UserInfoDB");
                        sweatPointSchema.addField("sweatPoints",Integer.class);

                oldVersion++;
            }

            if(oldVersion == 2){
                RealmObjectSchema teamInfoSchema = schema.get("TeamInfoDB");
                        teamInfoSchema.addField("teamDescription",String.class);

                oldVersion ++;
            }

            if(oldVersion == 3){
                RealmObjectSchema leaderboardInfoSchema = schema.get("LeaderBoardDb");
                            leaderboardInfoSchema.addField("displayName",String.class);


                oldVersion ++;

            }

        }
    }

public class LeaderBoardDb extends RealmObject {
    private String percentile;

    private String rank;

    private String details;

    private String email;

    @PrimaryKey
    private String userId;

    private String name;

    private String rewards;

    private String sweatPointEarned;

    private String photoUrl;

    private String note;


    private String displayName;
}

我在这里省略了getter和setter,但它们已经设置好了。 现在每当我运行我的应用程序时,我得到如下的错误堆栈

03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime: FATAL EXCEPTION: main
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime: Process: com.workoutguru.workoutcash, PID: 22111
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.workoutguru.workoutcash/com.workoutguru.workoutcash.DrawerActivity}: java.lang.IllegalArgumentException: Field already exists in 'TeamInfoDB': teamDescription
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:175)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5602)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:  Caused by: java.lang.IllegalArgumentException: Field already exists in 'TeamInfoDB': teamDescription
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.RealmObjectSchema.checkFieldNameIsAvailable(RealmObjectSchema.java:498)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.RealmObjectSchema.checkNewFieldName(RealmObjectSchema.java:484)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.RealmObjectSchema.addField(RealmObjectSchema.java:142)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.workoutguru.workoutcash.realmDB.Migration.migrate(Migration.java:27)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.BaseRealm$2.onResult(BaseRealm.java:604)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.RealmCache.invokeWithGlobalRefCount(RealmCache.java:242)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.BaseRealm.migrateRealm(BaseRealm.java:591)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.Realm.migrateRealm(Realm.java:1224)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.Realm.migrateRealm(Realm.java:1213)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.Realm.createInstance(Realm.java:237)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:114)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at io.realm.Realm.getDefaultInstance(Realm.java:181)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.workoutguru.workoutcash.DrawerActivity.exportDatabase(DrawerActivity.java:268)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.workoutguru.workoutcash.DrawerActivity.onCreate(DrawerActivity.java:207)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5451)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:175) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5602) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
03-24 13:09:53.191 22111-22111/com.workoutguru.workoutcash E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

0 个答案:

没有答案