How to cancel/unsubscribe from coroutines Flow

前端 未结 3 1189
既然无缘
既然无缘 2021-02-14 03:23

I notice a strange behavior when trying to prematurely cancel from a Flow. Take a look at the following example.

This is a simple flow that emits integer values

3条回答
  •  天命终不由人
    2021-02-14 03:53

    I came up with this recently

    it seems that it will only actually cancel if it reaches a suspending point and in your code that emits there is no such point

    to solve this either add yield() between emissions or some other suspending function like delay(100)

提交回复
热议问题