2016-09-30 6 views
0

새로운 RealmObject 및 매개 변수를 사용하여 이전 Realm 구조를 새 구조로 마이그레이션하려는 문제가 있습니다. 즉, 앱이 이미 Google Play에 있으므로 사용자가 이미 일부 데이터를 특정 테이블에 저장했기 때문입니다. 목표는 영역 데이터베이스를 삭제하고 다른 곳에 저장하기 전에 데이터를 복원하는 것입니다. 나는 지금 당장해야 할 일에 대한 딜레마에 빠져 있습니다.새 버전으로의 영역 백업 및 마이그레이션

이 문제를 해결하려고, 나는 내 응용 프로그램 클래스에서 다음을 구현 :

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을 해결하여 앱이 올바르게 시작됩니다.

관련 문제