Closed
Description
Hello,
usually when I want a list, I use a function returning a Flow
followed by toList()
which is working perfectly.
Here is what I write now:
interface UserRepo : CoroutineCrudRepository<User, Long> {
suspend fun findByEmail(email: String): User?
fun findByFirstName(firstName: String): Flow<User>
}
// in use:
val users = userRepo.findByFirstName("Max").toList() // toList() is the build-in suspended function
What about including this into the repository itself? Then the code is much cleaner and because toList() is a suspended function, the thread is not blocked anyways.
interface UserRepo : CoroutineCrudRepository<User, Long> {
suspend fun findByEmail(email: String): User?
suspend fun findByFirstName(firstName: String): List<User>
}
Currently an exception is thrown when I try the 2nd example.