So I'm trying to completely clear my tables upon user log out but deleting the tables does not seem to stick and the old data is still there when another user logs in. I'm using RXJava Completables and Room and my code resembles the following.
mDbManager.deleteAllFromTable(mCompositeDisposable)
.doOnComplete(new Action() {
@Override
public void run() {
//Finish logging out.
}
})
.subscribe();
Method in manager looks like
@Override
public Completable deleteAllFromTable(final CompositeDisposable compositeDisposable) {
return Completable.fromAction(new Action() {
@Override
public void run() {
mContactDao.deleteAllFromTable();
vacuumTables(compositeDisposable);
}
}).subscribeOn(mSchedulerProvider.io())
.observeOn(mSchedulerProvider.ui());
}
Method in Dao looks like
@Query("DELETE FROM table")
void deleteAllFromTable();
I've tried it without vacuuming the db and marking the Query as a Transaction and also creating an abstract method for the delete that also vacuums the table and marking that as a Transaction but the data still persists and the deleting is not finished when doOnComplete is called. Another thing I want to point out is that when deleting from the table there are other tables with data linked by foreign keys that are deleted as well.
Instead of manually deleting all records from your tables, you can also use RoomDatabase.clearAllTables()
method that will delete all rows from all the tables that are registered to this database as entities()
.
For more details, see this https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.html#clearAllTables()
In DataBase
fun clearTables() {
GlobalScope.launch(Dispatchers.IO) {
[email protected]()
}
}
call the function in ViewModel
YourDataBase.getInstance(mContext).clearTables()
Invoke method clearAllTables() from the class that extends Roomdatabase which Deletes all rows from all the tables that are registered to this database as entities().
Check the documentation here https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase
Use clearAllTables() with RXJava like below inorder to avoid java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
getRoomDatabase().clearAllTables();
}
}).subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribe(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "--- clearAllTables(): run() ---");
getInteractor().setUserAsLoggedOut();
getMvpView().openLoginActivity();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.d(TAG, "--- clearAllTables(): accept(Throwable throwable) ----");
Log.d(TAG, "throwable.getMessage(): "+throwable.getMessage());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With