I have an app that’s using SQLCipher + Room and I ...
# compose
a
I have an app that’s using SQLCipher + Room and I want to ask the user for the password again when they return after leaving the app so I close the database. When they leave the single Activity app, in
onStop
, I close the database. When they return, I use the Navigation component to direct them to the Password Screen. The screen with the list of data uses a
ViewModel
that exposes Flow from a Room DAO. The problem I’m having is that observing this from my
ItemList
Composable apparently leaks the DAO (and hence the Database instance) when the user leaves, and when the user unlocks the app again, the
ItemList
is still collection this Flow, which makes the app crash. Using
asLiveData
on the
Flow
fixes the issue. Am I doing something wrong by observing the Room
Flow
from a
Composable
like this or is this some weird bug?
h
Could you show code?
a
Sure. Simplified as much as I could. https://gist.github.com/AfzalivE/03f1034c42a39f1563f60be25d112bdb The NavGraph code could use some improvement admittedly.
h
How about add to repository some checking is db authenticated or not? For example you can set db to null, when you close it, and return empty flow in repository if db is null
Alternatively you can not close db at all. Use boolean flag in Singleton and check it every onStart, set on onStop and in db creation by passphase
a
How about add to repository some checking is db authenticated or not? For example you can set db to null, when you close it, and return empty flow in repository if db is null
Thanks, that’s a good idea! I’ll try to see if that works.
Alternatively you can not close db at all. Use boolean flag in Singleton and check it every onStart, set on onStop and in db creation by passphase
Feel like not closing the DB might make it a bit unsecure (insecure?), not sure but I’ll definitely try the first suggestion!