Objectbox,如何在冲突上掉落所有的迁移?
在我的情况下,可以保存的数据可以在不担心模型更改的情况下清理,这是一种更好的方法来做到这一点?
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)
}
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种方法有点骇人听闻,但是为什么不呢!我的意思是,如果您不在乎失去现有数据...
我可以建议的两个可能的改进:
boxfor()
应该在之外尝试
,因为这里的任何错误都会与模型更改无关。异常
。例如dbschemaexception
用于模型相关的异常,或作为“内在”解决方案,dbexception
用于捕获任何与DB相关的异常(包括dbschemaexception
) 。因此,要调整您的代码,它看起来可能是这样:
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:
boxFor()
should be outside thetry
because any error here would be unrelated to model changes.Exception
, you could be more specific. E.g.DbSchemaException
for model related exceptions, or as an "in-between" solution,DbException
for catching any DB-related exceptions (includingDbSchemaException
).So, to adapt your code it could look like this: