Search code examples
androidrealm

Caused by: java.lang.IllegalArgumentException: Realm on disk is newer than the one specified: v3 vs. v0?


How to solve this issue, i searched for result i dont't know how to solve the result.

I am using broadcast receiver to receive data

   @Override
    public void onReceive(Context context, Intent intent) {

        Realm.init(context);
        realm = Realm.getDefaultInstance();
}

In broadcast receiver class i have initialized the realm. The issue is occurring at line ` realm = Realm.getDefaultInstance();. Please help me how to solve this.

The realm initializaion in oncreate() method of the application is

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);
        Realm.init(this);
//        RealmConfiguration config = new RealmConfiguration.Builder()
//                .deleteRealmIfMigrationNeeded()
//                .build();
        RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(3) // Must be bumped when the schema changes
                .migration(new Migration()) // Migration to run
                .build();

        Realm.setDefaultConfiguration(config);
//        Realm.deleteRealm(config);
        realm.getDefaultInstance();

I have initialized the realm in the activity.Please help me how to solve this.


Solution

  • change

    @Override
    public void onReceive(Context context, Intent intent) {
    
        Realm.init(context);
        realm = Realm.getDefaultInstance();
    

    to

    @Override
    public void onReceive(Context context, Intent intent) {
    
        Realm.init(context);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(SCHEMA_VERSION) // Must be bumped when the schema changes
                .migration(new Migration()) // Migration to run
                .build();
        realm = Realm.getInstance(config);
    

    in your broadcast receiver.

    And

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(SCHEMA_VERSION) // Must be bumped when the schema changes
                .migration(new Migration()) // Migration to run
                .build();
    

    Move SCHEMA_VERSION property somewhere where it's a public static final int (constant).