Skip to content

Allow suspend + List in CoroutineCrudRepository #395

Closed
@Tienisto

Description

@Tienisto

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions