blonsky95 / DigiCoachAndroid

0 stars 0 forks source link

Create databases unique to user - imagine 2 users in one phone #75

Closed blonsky95 closed 4 years ago

blonsky95 commented 4 years ago

So basically if 2 users log into one same phone, I dont want the app to download all the info for each. Instead each of the users will be storing all their stuff in a different database, and then depending on the user id it will access its own db.

blonsky95 commented 4 years ago

add it as parameter in AppRoomDatabase and I think that should be it

blonsky95 commented 4 years ago

https://stackoverflow.com/questions/53416907/room-with-multi-user-database-in-single-android-app

blonsky95 commented 4 years ago

WORKING!!!!!

So the gist on this one was actually not adding the userid to the parameter to build the custom database (that was the easy thing). But the problem is when you dont close the app completely the db instance doesnt die, so when you log out and use a different user, the build database is not called because "instance" (of db) is not null and thus no need to create a different one.

To tackle this I created a

       fun destroyInstance() {
            if (instance?.isOpen==true) {
                instance?.close()
            }
            instance=null
        }

function which when called, destroys the instance/makes it null, this function atm is only called by the log out function, which is triggered from HomeViewModel, which is triggered by the log out selectable item in the top right corner menu.