Reputation: 9390
suspend fun showOrder(){
flow<Int> {
for (i in 0..10){
emit(i)
delay(1000)
}
}.collect {
Timber.d("test $it")
}
}
I got only first emition : "0" and that is all. What is the problem?
I call this function from android
Upvotes: 0
Views: 2152
Reputation: 1038
The reason it is only collected once is that collect is like callback and your suspend function is executing linearly. Untill second time collect it called it is already out of scope.
Upvotes: 0
Reputation: 9390
I was calling it in response to notification. If notification is from Foreground( Firebase) than function was scoped to that service. Since the lifeTime of that service was very small, my function execution was canceled. And this was the reason for such behavior. So I bounded scope to MainActivity and now it works as expected.
Upvotes: 0
Reputation: 446
The code is valid. The most possible problem is that you are running it in a test and the program finishes before the flow is collected. Try wrapping it in runBlocking
.
Upvotes: 1