2016-08-24 91 views
1

我已更新至最新版本,希望能解決我的問題,但事實並非如此。我正在使用沒有任何工作。 Realm Migration [Android 1.2]

RealmConfiguration config = new RealmConfiguration.Builder(this) 
      .name("myrealm.realm") 
      .migration(new Migration()) 
      .schemaVersion(2) // 2 
      .build(); 
    try { 
     Realm realm = Realm.getInstance(config); // Automatically run migration if needed 
     realm.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Realm.setDefaultConfiguration(config); 

此代碼用於更新並添加一些新對象。這裏是我的移民

public class Migration implements RealmMigration { 
@Override 
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) { 
    // Access the Realm schema in order to create, modify or delete classes and their fields. 
    RealmSchema schema = realm.getSchema(); 
    // Migrate from version 1 to version 2 
    if (oldVersion == 1) { 
     // Create a new classes 
     RealmObjectSchema styleSchema = schema.create("SavedStyle").addField("title", String.class).addField("json", String.class); 
     RealmObjectSchema dictSchema = schema.create("SavedDictionary").addField("title", String.class).addField("dictionary", String.class); 
     RealmObjectSchema journalSchema = schema.create("CustomJournal").addField("title", String.class).addField("json", String.class); 
     oldVersion++; 
    } 
    if (oldVersion < newVersion) { 
     throw new IllegalStateException(String.format("Migration missing from v%d to v%d", oldVersion, newVersion)); 
    } 
} 

}

我得到的錯誤

java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. The most likely cause is that equals() and hashCode() are not overridden in the migration class: otherClasses.Migration 

如果只是想而不從以前的版本更新,即使運行它的獨立。不知道該做什麼。真的需要它的工作,所以我不必擦除每個人的數據。考慮製作更正式的錯誤報告,但想檢查是否有其他人知道是否有解決方案。每當我嘗試獲取默認配置時,都會發生此問題。它通常工作當我第一次打開應用程序,但是當我去到下一個活動

+0

我就像你可能看到過的那樣,但你從來沒有反應過。我想這個問題並不重要。 – EpicPandaForce

回答

1

我想說的錯誤信息是非常具體的崩潰,它說,你應該做到以下

public class Migration implements RealmMigration { 
    @Override 
    public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) { 
     //... 
    } 

    @Override 
    public boolean equals(Object object) { 
     return object != null && object instanceof Migration; 
    } 

    @Override 
    public int hashCode() { 
     return Migration.class.hashCode(); 
    } 
} 
相關問題