Android Room-自动递增@database版本号? [英] Android Room - auto increment @database version number?

查看:572
本文介绍了Android Room-自动递增@database版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发过程中,我的Room db模式非常不稳定.每次对架构进行任何更改时,都需要更新我的版本号,就像这样;

During development my Room db schema is very volatile. Every time I do any change to the schema I need to update my version number, like this;

@Database(version = 27,
    entities = {MyClass.class})

public abstract class MyDatabase extends RoomDatabase

我也在使用fallbackToDestructiveMigration;

I am using fallbackToDestructiveMigration too;

Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, SystemStrings.ROOM_DATABASE_NAME)
                // allow queries on the main thread.
                // Don't do this on a real app! 
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

有什么方法可以避免为每个小"更改更新版本号?正如我所说,目前情况非常不稳定.

Is there any way to avoid updating the version number for each "little" change? As I said, things are quite volatile right now.

推荐答案

删除数据库将导致每次运行该应用程序时都要对其进行初始化/构建.

Deleteing the database would result in it being initialised/built each time the App is run.

您可以使用:-

context.getApplicationContext().deleteDatabase(SystemStrings.ROOM_DATABASE_NAME); //<<<< ADDED before building Database.
//Your existing code follows 
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, SystemStrings.ROOM_DATABASE_NAME)
                // allow queries on the main thread.
                // Don't do this on a real app! 
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build();

当然,您不会在真正的应用程序中拥有这个

这篇关于Android Room-自动递增@database版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆