我们是否应该取消Applicationclass中创建的applicationScope?
Should we cancel the applicationScope created in the Application class?
我在我的 Android 应用程序 class 中创建了一个 applicationScope,用于应该比 viewModelScope 和 lifecycleScope 更有效的操作,如下所示:
class App : Application() {
val applicationScope = CoroutineScope(...)
我们是否应该自动取消 applicationScope 以避免泄漏或其他原因?我问是因为我见过一些人们会打电话给
的项目
applicationScope.cancel()
当他们的主要 activity 将被销毁或当用户想要关闭应用程序时。这在某些情况下是否有必要?
无需取消应用程序级范围。当进程被杀死时,它将被拆除。 Source
class MyApplication : Application() {
// No need to cancel this scope as it'll be torn down with the process
val applicationScope = CoroutineScope(SupervisorJob() + otherConfig)
}
We don’t need to cancel this scope since we want it to remain active as long as the application process is alive, so we don’t hold a reference to the SupervisorJob. We can use this scope to run coroutines that need a longer lifetime than the calling scope might offer in our app.
我在我的 Android 应用程序 class 中创建了一个 applicationScope,用于应该比 viewModelScope 和 lifecycleScope 更有效的操作,如下所示:
class App : Application() {
val applicationScope = CoroutineScope(...)
我们是否应该自动取消 applicationScope 以避免泄漏或其他原因?我问是因为我见过一些人们会打电话给
的项目applicationScope.cancel()
当他们的主要 activity 将被销毁或当用户想要关闭应用程序时。这在某些情况下是否有必要?
无需取消应用程序级范围。当进程被杀死时,它将被拆除。 Source
class MyApplication : Application() {
// No need to cancel this scope as it'll be torn down with the process
val applicationScope = CoroutineScope(SupervisorJob() + otherConfig)
}
We don’t need to cancel this scope since we want it to remain active as long as the application process is alive, so we don’t hold a reference to the SupervisorJob. We can use this scope to run coroutines that need a longer lifetime than the calling scope might offer in our app.