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?

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.

behaviour does not change when using

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

and use the autoDispose(scopeProvider) instead


Solution

  • The corresponding lifecycle event in your example would be onDestroyView(), which is after both onPause() and onStop(). See this diagram for the lifecycle flow: https://i.sstatic.net/maMyK.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)

    2 - Manually set the desired end event

    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")}
          }
       }
    }