Realm not auto-deleting database if migration needed

不想你离开。 提交于 2019-12-02 05:24:46

问题


We are in development and db schema changes occur often. Since we are not live, migrations are not needed. I therefor configured Realm as follows:

RealmConfiguration config = new RealmConfiguration.Builder(context)
                .name("jt.realm")
                .schemaVersion(1)
                .deleteRealmIfMigrationNeeded() // todo remove for production
                .build();
        Realm.setDefaultConfiguration(config);

However, when the schema is changed, an exception is thrown: RealmMigration must be provided

My understanding from the docs are that the Realm should auto-delete the db since deleteRealmIfMigrationNeeded() is present in the config, but this does not seem to be happening. Why is this occurring?

Android Studio Dependency

compile 'io.realm:realm-android:0.86.1'


回答1:


We had a similar issue. We solved this by adding

Realm.getInstance(config)

right after

Realm.setDefaultConfiguration(config);

We think the configuration will be set up after Realm is called the first time. This time we don't use any Realm object so there's no exception.




回答2:


Super late answer by in case anyone has same problem you can look at what I did based on Dagger2.

First create a module:

@Module
public class DatabaseModule {

    public DatabaseModule(Context context) {
        Realm.init(context);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("mydb.realm")
                .schemaVersion(1)
                .deleteRealmIfMigrationNeeded()
                .build();

        Realm.setDefaultConfiguration(config);
    }

    @Provides
    Realm provideRealm() {
        return Realm.getDefaultInstance();
    }
}

Its Context is comming from another module

@Module
public class ApplicationModule {
    private Application application;

    public ApplicationModule(Application application) {
        this.application = application;
    }

    @Provides
    @Singleton
    Context provideContext() {
        return application;
    }
}

This is you App level component:

@Singleton
@Component(modules = {ApplicationModule.class, DatabaseModule.class, NetworkModule.class})
public interface ApplicationComponent {

    void inject(HomeActivity activity);
}

Finally use it in your Activity/Fragment:

public class HomeActivity {

@Inject Realm mRealm;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupDependencyInjection();
        setContentView(R.layout.activity_home);

        // Now you can use Realm
    }

    private void setupDependencyInjection() {
        ((YourApplication) getApplication())
                .getAppComponent()
                .inject(this);
    }
}


来源:https://stackoverflow.com/questions/34961456/realm-not-auto-deleting-database-if-migration-needed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!