[英]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.