CoroutineAutoDispose icon indicating copy to clipboard operation
CoroutineAutoDispose copied to clipboard

Suggested update to documentation - clarify usage

Open alteredworlds opened this issue 5 years ago • 0 comments

Thanks for the library!

It would be helpful to note the following information in your documentation: It is important to use view.autoDisposeScope.launch {} and then afterwards switch to an alternate Dispatcher, rather than specifying an alternate Dispatcher with the launch. For example, the job resulting from view.autoDisposeScope.launch(Dispatchers.IO){} will not be cancelled as one might expect, when the view is detached.

Reason: When you specify the Dispatcher in e.g. launch(Dispatchers.IO){}, the ViewAutoDisposeInterceptorImpl is removed from the resulting CoroutineContext and view.autoDispose(job) is never called.

It is my understanding that can be only one entry in the CouroutineContext for ContinuationInterceptor.Key, so I don't quite understand if the following code really means that coroutines started via autoDisposeScope.launch { } will be on Dispatchers.Main?

val newScope = ViewCoroutineScope(
      SupervisorJob() +
        Dispatchers.Main +
        autoDisposeInterceptor()
    )

It seems more likely that Dispatchers.Main is simply replaced by autoDisposeInterceptor(), but then what does this mean with regards to what thread can be expected - e.g. is UI access safe? Whenever I have put in a breakpoint within a coroutine created via autoDisposeScope.launch { }, it has thus far always been on the main thread. Interested to find out more.

alteredworlds avatar Jul 30 '20 14:07 alteredworlds