Skip to content

Commit

Permalink
Add Lifecycle observer only on next event loop
Browse files Browse the repository at this point in the history
This allows to support use cases like creating a scope active while
STARTED from an Activity onStart() method.
  • Loading branch information
LouisCAD committed Nov 2, 2018
1 parent 8096112 commit 7f063c6
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions integration/kotlinx-coroutines-androidx-lifecycle/src/Lifecycle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import androidx.lifecycle.GenericLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.INITIALIZED
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.*

/**
* Returns a [CoroutineScope] that uses [Dispatchers.Main] by default, and that will be cancelled as
Expand All @@ -34,14 +31,19 @@ fun Lifecycle.createJob(activeWhile: Lifecycle.State = INITIALIZED): Job {
"DESTROYED is a terminal state that is forbidden for createJob(…), to avoid leaks."
}
return SupervisorJob().also { job ->
if (!currentState.isAtLeast(activeWhile)) job.cancel()
else addObserver(object : GenericLifecycleObserver {
override fun onStateChanged(source: LifecycleOwner?, event: Lifecycle.Event) {
if (!currentState.isAtLeast(activeWhile)) {
removeObserver(this)
job.cancel()
}
when (currentState) {
Lifecycle.State.DESTROYED -> job.cancel() // Fast path if already destroyed
else -> GlobalScope.launch(Dispatchers.Main) { // State is usually synced on next loop,
// this allows to use STARTED from onStart in Activities for example.
addObserver(object : GenericLifecycleObserver {
override fun onStateChanged(source: LifecycleOwner?, event: Lifecycle.Event) {
if (!currentState.isAtLeast(activeWhile)) {
removeObserver(this)
job.cancel()
}
}
})
}
})
}
}
}

0 comments on commit 7f063c6

Please sign in to comment.