Question about Flow together with Room databases. ...
# android
m
Question about Flow together with Room databases. With Room+Flow, the query is “refreshed” whenever new data arrives, so keeping a list of items up to date is really easy, but what if the list depends on time as well, so the records in the database contains a timestamp and I want to show (with flow) the records from the last 5 minutes, updated every minute, is there anyway I can re-trigger the query manually, with flow or other techniques?
l
If I understand well, I think you can use
flatMapLatest
for that use case.
m
Not sure if that would help in this case. I would like to “refresh” the query or filter the output flow based on time intervals. basically, I get a flow from ROOM, then every minute, even if ROOM doesnt trigger, I would like to filter that flow to only contain the last 5 minutes of data.
l
You can create a
flow { … }
with a loop and calls to
delay
or whatever logic and use
combine
for example.
m
can that react immediate when the “source” flow changes, or will that limit the throughput to the speed set by
delay
?
Think of it as a calendar view that shows your items for
today
, the sql query makes sure you get the items for
today
, If anything is added for today, it should show up immediately, but at midnight I want to ‘re-trigger’ ROOM (or just filter out
yesterday
from the flow.
c
You might
merge()
two flows: the one directly subscribed to Room, and the one on a timer which makes one-time queries to Room on each tick. Results from both would flow downstream https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/merge.html
m
Thanks for the hint. I'll check it out tomorrow (it's late here ;) )
l
@Mikael Alfredsson if you use an operator of the xxxLatest family at the right place, it will cancel the ongoing delay call and feed the new value.
☝️ 1