https://kotlinlang.org logo
Title
m

Mohamed Ibrahim

11/20/2020, 12:27 PM
Hello there, if
repositoryContract.retrieveTopStories()
is a suspend function which returns
List<Story>
why I can’t convert it to Flow by calling
repositoryContract.retrieveTopStories().asFlow()
j

John O'Reilly

11/20/2020, 12:36 PM
What issue are you seeing? I just tried something similar here and it seemed to work
e

elizarov

11/20/2020, 12:37 PM
Flow is clod. You have to perform the action when collected, so the proper way is to:
flow {
    repositoryContract.retrieveTopStories()
        .forEach { emit(it) } 
}
👍 1
j

John O'Reilly

11/20/2020, 12:43 PM
somewhat contrived but just tried following and that seems to work fine.....though maybe I misunderstood ask
fun main()  = runBlocking {
    val api = PeopleInSpaceApi()

    api.fetchPeople().asFlow().collect {
        println(it)
    }
}
c

Chantry Cargill

11/20/2020, 1:02 PM
Could also be missing the kotlinx couroutines library.
g

gildor

11/20/2020, 2:29 PM
If you want to convert it to flow which emits whole list, you need method reference: repositoryContract::retrieveTopStories.asFlow()
👍 1