Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting data from Room Database

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.

like image 278
Seef Avatar asked Jul 26 '18 17:07

Seef


4 Answers

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()

like image 172
Qasim Avatar answered Nov 09 '22 22:11

Qasim


In DataBase

fun clearTables() {
        GlobalScope.launch(Dispatchers.IO) {
            [email protected]()
        }
    }

call the function in ViewModel

  YourDataBase.getInstance(mContext).clearTables()
like image 44
Muhammed shamshad p Avatar answered Nov 09 '22 20:11

Muhammed shamshad p


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

like image 2
nishon.tan Avatar answered Nov 09 '22 22:11

nishon.tan


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());


            }
        });
like image 1
Adewale Balogun Avatar answered Nov 09 '22 20:11

Adewale Balogun