androidobjectboxobjectbox-androidobjectbox-java

ObjectBox, how to drop-everything migration on conflicts?


In my case saved data can be cleaned without fear in case of model changes, there is a better way to do this ?

private val dataSource = try {
    MyObjectBox.builder().androidContext(context).build().boxFor(XXXX::class)
} catch (e: Exception) {
    BoxStore.deleteAllFiles(context, BoxStoreBuilder.DEFAULT_NAME)
    MyObjectBox.builder().androidContext(context).build().boxFor(XXXX::class)
}

I'm wondering if there is such a thing as this: https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Builder#fallbackToDestructiveMigration()


Solution

  • This approach is a bit hacky, but why not!? I mean, if you don't care about loosing existing data...

    Two possible refinements I can suggest:

    So, to adapt your code it could look like this:

    private val store = try {
        MyObjectBox.builder().androidContext(context).build()
    } catch (e: DbSchemaException) {
        BoxStore.deleteAllFiles(context, BoxStoreBuilder.DEFAULT_NAME)
        MyObjectBox.builder().androidContext(context).build()
    }
    private val box = store.boxFor(XXXX::class)