I use Room primarily to convert SQL queries to `Fl...
# android
m
I use Room primarily to convert SQL queries to
Flow
. However, I prefer to work directly with the support library APIs. How to convert a generic query to a
Flow
(either using a 3rd party library or building from scratch)?
m
A
Flow
could be crazy complicated, as it implies initial results + results over time. Room as a metric ton of code around its
InvalidationTracker
for trying to pull that off. My guess is that the Room team would steer you towards
@RawQuery
. If you are simply looking for asynchronous queries, putting your
SupportSQLiteDatabase
call in a
suspend
function, with your
rawQuery()
(or whatever) wrapped in
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
probably suffices.
m
Thanks Mark. I absolutely need it for tracking changes over time. I was wondering whether it would be realistic to attempt to implement something using the usual (ancient?) approach of using
ContentProvider.notifyChange()
m
I'm uncertain where the
ContentProvider
is coming from. Regardless, if you want to go through the headache of ensuring that you emit updates manually, then you could try a
SharedFlow
both as a return value from your function and to hold onto in your repository (or whatever is doing the SQL calls). The repository would then need smarts in all of its other CRUD functions that if it is doing something that affects the results of your
Flow
-based query, you execute some code that fires a fresh query result into that
SharedFlow
. To be honest, I'm not really certain what this is buying you over
@RawQuery
, though.
m
My aim is to drop Room from my dependencies, and I’m trying to work out if that is feasible given I need to have several queries return as
Flow
i
I think you underestimate how much Room is doing for you
🚫 1
☝️ 2
m
Not at all. I’m just evaluating.