2016-09-30 13 views
0

新しいRealmObjectとパラメータを使用して、古いレルム構造を新しいレルム構造に移行しようとする問題に直面しています。つまり、アプリはすでにGoogle Playに入っているため、ユーザーは特定のデータをいくつかのテーブルに既に保存しています。目標は、レルム・データベースを削除して別の場所に保管する前にデータをリストアすることです。私は今何をすべきかについてのジレンマに陥っています。レルムのバックアップと新しいバージョンへの移行

これを解決しようとするが、私は私のApplicationクラスで次のように実装:

RealmMigration migration = new RealmMigration(){ 
    @Override 
    public void migrate(...){ 
     if(oldVersion == 0){ 
      //Get the data from realm and store somewhere else 
     } 
    } 
} 

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this) 
     .schemaVersion(1) 
     .migration(migration) 
     .deleteRealmIfMigrationNeeded() 
     .build(); 
Realm.setDefaultConfiguration(realmConfiguration); 
Realm.getInstance(realmConfiguration); 

ここで問題にそれをやって、方法deleteRealmIfMigrationNeeded()が実行されると、移行が()、その後、ではないということですデータベースからデータを取得する前にすべてのデータが失われます。私が望んでいたのは、アプリケーションをアップデートするときにデータベースからバージョンを取得し、それが古いバージョンであるかどうかを比較し、レルムのデータをファイルに格納してから、deleteRealmIfMigrationNeeded()を実行してRealmMigrationNeededException。

私はすでに次のリンクを見て:
How to backup Realm DB in Android before deleting the Realm file. Is there any way to restore the backup file?
Realm not auto-deleting database if migration needed
https://github.com/realm/realm-cocoa/issues/3583

答えて

1

私は移行()メソッドでは、適切なRealmSchemaを追加することで問題を解決しました。ような何か:

RealmMigration migration = new RealmMigration(){ 
    @Override 
    public void migrate(...){ 
     final RealmSchema = relam.getSchema(); 
     if(oldVersion == 0){ 
      if(!schema.get("Person").getPrimaryKey().equals("codePerson")){ 
       schema.get("Person") 
        .removePrimaryKey() 
        .addPrimaryKey("codePerson"); 
      } 

      //There are other similar statements here 
     } 
    } 
} 

は、それから私はRealmConfigurationからメソッドdeleteRealmIfMigrationNeeded()を削除しました。

RealmMigrationNeededExceptionが解決されたため、アプリケーションが正しく開始しています。

関連する問題