Given a room Database and a request with a DateTime based parameter :
@Transaction
@Query("SELECT * FROM itemModel WHERE datetime(datetime(`end`), 'localtime') > datetime(:minDate, 'localtime') ORDER BY `end` ASC")
fun getFlow(
minDate: LocalDateTime,
): Flow<List<ItemModel>>
=> Based on the minDate
param, data returned from the request should change over time (even if database has no data changes)
How to handle flow updates ? Because :
I init my flow like this :
var items: StateFlow<List<ItemModel>>
fun initItemsFlow(context: Context) {
items = getAllRecords(context).stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = emptyList(),
)
}
Should I manually call initItemsFlow
again when I need (= re-seting from scratch the whole flow) or is there a better flow/room mechanims to handle that ?
Thanks
I don't know if that is just caused by a copy&paste error but the dao's getFlow
needs a LocalDateTime
parameter where in the (I guess) view model you only call getAllRecords
with a Context
parameter. It is unclear how the functions are related so in the rest of my answer I will ignore getAllRecords
because it doesn't seem to serve any purpose that is relevant to the problem at hand.
Instead I will outline how you can change the getFlow
's minDate
parameter while still returning the same StateFlow. You can then adapt this to your actual code.
The solution is to introduce another flow – a MutableStateFlow – to store the minDate. A MutableStateFlow is like a variable, it represents a single value that can be changed, so whenever you want to update minDate you can just set the MutableStateFlow's value. Since a MutableStateFlow is also a flow you can transform its value using any of the flow transformation functions. In this case you want to switch out the flow with the one returned from getFlow
.
Assuming you want to expose your database flow in the view model and also use it to update the minDate
parameter then what I described above could look like this:
class MyViewModel(
dao: MyDao,
) : ViewModel() {
private val minDate = MutableStateFlow<LocalDateTime?>(null)
val items: StateFlow<List<ItemModel>> = minDate
.flatMapLatest {
if (it == null) flowOf(emptyList())
else dao.getFlow(it)
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = emptyList(),
)
fun updateMinDate(date: LocalDateTime) {
minDate.value = date
}
}
Here minDate
is the new MutableStateFlow. It is used as the basis for the items
property and flatMapLatest
is used to switch to another flow based on minDate
's content. That transforms the Flow<LocalDateTime>
to a Flow<List<ItemModel>>
by calling getFlow
with the MutableStateFlow's value as parameter.
Then that flow is converted to a StateFlow, ready to be collected by the UI. You should use the viewModelScope
for that and not some other scope. Note that you do not need to switch to the IO dispatcher here, that is done internally by the Room library.
Now, whenever you call updateMinDate
the items
flow is automatically updated with this new date. Also any changes in the databse will cause the StateFlow to update. This is the usual way how to modify a flow without leaving the world of flows.