Does RoomDB supports dropAllTables() and createAllTables()?

痞子三分冷 提交于 2020-06-17 09:15:14

问题


GreenDAO supports DaoMaster.dropAllTables() and DaoMaster.createAllTables(). I'm looking for similar functionality in RoomDB. Does RoomDB supports this functionality ??

The use-case for this functionality is, when the user tries to login with new mobile number to my app, i want to clear the data of old login number by showing a warning dialog message and allow login with new number.


回答1:


Room supports dropping and creating tables only during a migration between schema versions.

You can gain access to the underlying SupportSQLiteDatabase via RoomDatabase.getOpenHelper().getReadableDatabase(). With that, you can use execSQL() to execute SQL statements, include table dropping and creating.

But doing so is not consistent with the intended use of Room and is not safe. It will very likely break the InvalidationTracker used to notify observers of changes to managed tables.

From your use-case, it seems like rather than dropping and recreating the tables, all you need to do is clear them by deleting all entries.



来源:https://stackoverflow.com/questions/62405921/does-roomdb-supports-dropalltables-and-createalltables

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