繁体   English   中英

当 scope 处于 ON_PAUSE 或 ON_STOP state 时,使用 Autodispose 的流是否应该停止发射?

[英]Should streams using Autodispose stop emitting when scope is in ON_PAUSE or in ON_STOP state?

In autodispose library is it by design that when we have a stream with interval operator and autodispose the stream continues to trigger even when the scope has emitted a pause and stop state?

例子

Fragment {

   override fun onViewCreated() {
      Flowable.just(1).flatMap{ value -> 
         Flowable.interval(1, TimeUnit.SECONDS).map{value}
         .autoDispose(viewLifecycleOwner)
         .subscribe{Timber.d("Value: $value")}
      }
   }
}

当我们更改为下一个活动时,即使 scope 它自己转到 ON_PAUSE state 然后到 ON_STOP state,间隔也会继续发出值。

使用时行为不会改变

    private val scopeProvider by lazy { AndroidLifecycleScopeProvider.from(viewLifecycleOwner) }

并改用 autoDispose(scopeProvider)

在您的示例中,相应的生命周期事件将是onDestroyView() ,它位于onPause()onStop()之后。 请参阅此图了解生命周期流程: https://i.imgur.com/0EVReuq.png

如果您希望它在暂停或停止状态下停止,您可以

1 - 将此订阅移动到onStart() (用于停止)或onResume() (用于暂停)

2 - 手动设置所需的结束事件

Fragment {

   override fun onViewCreated() {
      Flowable.just(1).flatMap{ value -> 
         Flowable.interval(1, TimeUnit.SECONDS).map{value}
         .autoDispose(viewLifecycleOwner, untilEvent = Lifecycle.Event.ON_PAUSE) // or ON_STOP
         .subscribe{Timber.d("Value: $value")}
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM