Introduce LifecycleCoroutineScopeFactory
Closed this issue · 0 comments
RBusarow commented
If using LifecycleCoroutineScope
but not using the lifecycleScope
extension, the current best option for providing a custom scope/context via DI like Dagger may look like this:
class SomeFragment @Inject constructor(
coroutineScope: MainImmediateCoroutineScope
) : Fragment() {
private val lifecycleScope = LifecycleCoroutineScope(this, coroutineScope)
// ...
}
This isn't terrible, since everything important is in the injected scope, but it can be improved.
class SomeFragment @Inject constructor(
lifecycleCoroutineScopeFactory: LifecycleCoroutineScopeFactory
) : Fragment() {
private val lifecycleScope = lifecycleCoroutineScopeFactory.create(this)
// ...
}