I'm creating an application and I just added Room database. I need to compare a Long type date from the database with the current date, also of type Long. The problem is that when I query to retrieve the last record in the database:
@Query("SELECT dateOfEmotion FROM user_mood " +
"ORDER BY id DESC " +
"LIMIT 1;")
fun getLast(): Flow<Long>
Here's the code storing the variables that will be compared:
val today = System.currentTimeMillis() //Long
val lastRecord = viewModel.getLast() //Flow<Long>
in viewModel i have this:
fun getLast(): Flow<Long> { return repo.getLast() }
in repository this:
override fun getLast(): Flow<Long> { return dao.getLast() }
The issue lies with the data type Flow. How can I compare these two variables?
I have no idea how to approach this. I would appreciate any help.
You can collect() on the flow and the lambda you pass in will be called when the flow has a value. Just be careful that collect will block until finished (so don't use it on the main thread) and make sure the flow terminates or collect will block forever waiting for new values.
Really you should be using suspend and coroutines. If that means you need to fix your architecture, then fix it.