RxJava2 + Room: data is not being inserted in DB after clearAllTables() call
Asked Answered
I

0

3

In my Android application after successful login I'm saving session info in Room, then I'm retrieving user information from BE and saving it too. Everything works fine. I can see saved information in database tables.

When user logs out from application all tables are being cleared with appDatabase.clearAllTables() method call.

The catch is that on subsequent login there's no information that is being inserted in DB but my Rx calls aren't throwing any errors.

I've tried to use logging and debug but everything looks like normal.

Logging shows following actions being performed: login -> get user info from BE -> save session -> save user.

When debugging I can see that user info is being handled in UserDao_Impl's insertUser() method.

In application I use RxJava2 version 2.2.2, Room persistence library version 1.1.1, dependencies are being provided with Dagger version 2.19.

Here's my code snippets:

LoginUserUseCase.java

public Completable execute(@NonNull LoginInfo loginInfo) {
    Completable loginAndSaveSession = sessionRepository.loginUser(loginInfo)
                                                    .flatMapCompletable(
                                                            session -> sessionRepository.saveSession(
                                                                    session));
    Completable getAndSaveUserInfo = userRepository.getRemoteUserInfo()
                                                   .flatMapCompletable(
                                                           user -> userRepository.saveUser(
                                                                   user));
    return loginAndSaveSession.andThen(getAndSaveUserInfo);
}

UserRepository.java

public Completable saveUser(@NonNull User user) {
    return Completable.fromAction(() -> userDao.insertUser(user));
}

UserDao.java

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertUser(User user);

LogoutUserUseCase.java

public Completable execute() {
    return Completable.fromAction(() -> appDatabase.clearAllTables());
}

UPDATE (ISSUE RESOLVED): wasted a day on this bug. Finally come to understanding that some other library can affect application's work with Room. Turned out that Android Debug Database library that I've used to peek inside the database on device has a bug that breaks DB after you opened it once with its help.

Returning back to Stetho.

Inarticulate answered 2/11, 2018 at 10:47 Comment(3)
Can't figure out how UserRepository#saveUser() can return Completable if UserDao#insertUser() return nothing (void). Do you have special wrapper?Waldo
@Waldo sorry, my bad. Somehow I've lost Completable.fromAction() that wrapped UserDao.insertUser(). Fixed codeInarticulate
For anyone stumbling to this in future. The Database Inspector from Android Studio under App Inspection has the same issue: ie. if you open the DB inspector in Android studio and a call to clearAllTables() was made in the app. The inserting into DB is compromised. Tested on Android Studio Bumblebee | 2021.1.1 Patch 2 Windows 10Blackness

© 2022 - 2024 — McMap. All rights reserved.