Zsolt.bertalan
07/13/2022, 4:36 PMoverride fun stop() {
GlobalScope.launch(mainContext) {
delay(DELAY_STOP)
job?.cancel()
job = null
}
}
First of all, is this correct? Is this the best I can do? Maybe, does it make sense to add this to the library?Arkadii Ivanov
07/13/2022, 5:00 PMsubscribe
and don't remember the Disposable. And also put a descriptive comment. With coroutines, perhaps this would by launching the job in the GlobalScope
.
Delaying the Destroy even sounds also interesting. Most likely I would create an extensions as follows:
fun Lifecycle.delayedDestroy(millis: Long): Lifecycle = TODO()
Zsolt.bertalan
07/13/2022, 6:18 PMArkadii Ivanov
07/13/2022, 8:49 PMArkadii Ivanov
07/13/2022, 8:52 PMArkadii Ivanov
07/13/2022, 8:52 PMArkadii Ivanov
07/13/2022, 8:55 PMdispose
method in the Executor
and call super.dispose()
after a certain delay.Arkadii Ivanov
07/13/2022, 8:56 PMoverride fun dispose() {
scope.launch {
delay(1000L)
super.dispose()
}
}
Zsolt.bertalan
07/14/2022, 7:49 AMZsolt.bertalan
07/14/2022, 9:46 AMArkadii Ivanov
07/14/2022, 9:53 AMZsolt.bertalan
07/14/2022, 10:03 AMoverride fun onCleared() {
topicStore::dispose
}
But I already tried that. The binding is already stopped when this is called.Arkadii Ivanov
07/14/2022, 10:04 AMZsolt.bertalan
07/14/2022, 10:10 AMZsolt.bertalan
07/14/2022, 10:10 AMArkadii Ivanov
07/14/2022, 10:13 AMtopicStore::dispose
is just a method reference, it doesn't actually call dispose
.
You could use the following to delay the disposal of the store:
override fun onCleared() {
GlobalScope.launch {
delay(DELAY_STOP)
topicStore.dispose()
}
}
Zsolt.bertalan
07/14/2022, 11:11 AMArkadii Ivanov
07/14/2022, 11:20 AMZsolt.bertalan
07/14/2022, 1:12 PMArkadii Ivanov
07/14/2022, 1:20 PMfun Lifecycle.delayedDestroy(millis: Long): Lifecycle {
val lifecycle = LifecycleRegistry()
when (state) {
Lifecycle.State.CREATED -> lifecycle.create()
Lifecycle.State.STARTED -> lifecycle.start()
Lifecycle.State.RESUMED -> lifecycle.resume()
Lifecycle.State.DESTROYED -> {
lifecycle.create()
lifecycle.destroy()
}
Lifecycle.State.INITIALIZED -> Unit
}
subscribe(
object : Lifecycle.Callbacks by lifecycle {
override fun onDestroy() {
GlobalScope.launch {
delay(millis)
lifecycle.onDestroy()
}
}
}
)
return lifecycle
}
Zsolt.bertalan
07/14/2022, 1:38 PMArkadii Ivanov
07/14/2022, 1:49 PMinitialState
argument to LifecycleRegistry(...)
function - filed https://github.com/arkivanov/Essenty/issues/56.
I have updated the snippet here