android - launchWhenStarted 和 repeatOnLifecycle(STARTED) 收集流的区别

标签 android kotlin kotlin-flow

As launchWhenStarted and repeatOnLifecycle(STARTED) provide completely different functionality (launchWhenStarted suspends the execution of the coroutine, and repeatOnLifecycle cancels and restarts a new coroutine), if the names of the new APIs were similar (for example, using launchWhenever for the restarting APIs), developers could’ve got confused and even use them interchangeably without noticing.


source
什么时候使用哪个更简单的解释是什么?

最佳答案

launchWhenStarted只是一次延迟。repeatOnLifecycle创建一个挂起点,充当处理程序,每次生命周期进入提供状态时运行提供的 block ,并在低于它时取消它(因此对于 STARTED 它在停止时发生)。

关于android - launchWhenStarted 和 repeatOnLifecycle(STARTED) 收集流的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71113597/

相关文章:

android - 为什么 FocusManager 在 AlertDialog 中不起作用?

kotlin - 在 Swift 中使用协程流

android - Kotlin 多平台移动 : Flow<List<*SomeModel*>> gets mapped to Flow<NSArray> in iOS

android - 为什么我的 Intent 启动一个空的 Activity 而不是正确的 Activity ?

Android - achartengine 条形图问题

android - Exoplayer 会在后台播放多长时间?

java - 如何仅在某些异步执行完成后才执行代码?

android - 删除 arraylist 中自定义对象的重复值

java - 让 AlertDialog 等待用户输入?

android - 按 isCompleted 更新房间中的实体并使用流问题获取所有数据