简体   繁体   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? 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?

example例子

Fragment {

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

When we change to the next Activity the interval continues emitting the value even though the scope it self goes to ON_PAUSE state and then to ON_STOP state.当我们更改为下一个活动时,即使 scope 它自己转到 ON_PAUSE state 然后到 ON_STOP state,间隔也会继续发出值。

behaviour does not change when using使用时行为不会改变

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

and use the autoDispose(scopeProvider) instead并改用 autoDispose(scopeProvider)

The corresponding lifecycle event in your example would be onDestroyView() , which is after both onPause() and onStop() .在您的示例中,相应的生命周期事件将是onDestroyView() ,它位于onPause()onStop()之后。 See this diagram for the lifecycle flow: https://i.imgur.com/0EVReuq.png .请参阅此图了解生命周期流程: https://i.imgur.com/0EVReuq.png

If you want it to stop in pause or stop states you can either如果您希望它在暂停或停止状态下停止,您可以

1 - Move this subscription to onStart() (for stop) or onResume() (for pause) 1 - 将此订阅移动到onStart() (用于停止)或onResume() (用于暂停)

2 - Manually set the desired end event 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