如何在Android中关闭屏幕后的Coroutine(活动/片段)在Android中关闭?
活动
类:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val vm = getViewModel<MyViewModel>()
vm.invoke()
}
}
inline fun <reified T : ViewModel> FragmentActivity.getViewModel(): T {
return ViewModelProvider(this)[T::class.java]
}
我的ViewModel中的代码
类看起来如下:
class MyViewModel : ViewModel() {
private val repository: Repository = Repository()
fun invoke() {
viewModelScope.launch {
repository.execute()
}
}
}
repository:
private class Repository {
suspend fun execute() {
val isSuccess = libraryFunction()
if (isSuccess) {
onSuccess()
}
}
suspend fun onSuccess() {
Log.d("Repo", "onSuccess called")
delay(1000)
}
}
来自无法修改的库中的函数:
private val someItems = listOf("1", "2", "3")
suspend fun libraryFunction(): Boolean = coroutineScope {
someItems.map { item ->
async {
processItem(item)
}
}.awaitAll()
true
}
suspend fun processItem(item: String) = withContext(Dispatchers.IO) {
delay(5000) // simulate processing
}
当accod>活动
/fragment
在执行onsuccess()
之前已关闭
onSuccess called
。
在某些情况下,即使活动
/fragment
在关闭之前,我也需要执行onsuccess()
功能。
不使用globalsCope
,服务
和workmanager
类而无需使用globalsCope
可以做到吗?
Activity
class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val vm = getViewModel<MyViewModel>()
vm.invoke()
}
}
inline fun <reified T : ViewModel> FragmentActivity.getViewModel(): T {
return ViewModelProvider(this)[T::class.java]
}
The code in my ViewModel
class looks like the following:
class MyViewModel : ViewModel() {
private val repository: Repository = Repository()
fun invoke() {
viewModelScope.launch {
repository.execute()
}
}
}
Repository:
private class Repository {
suspend fun execute() {
val isSuccess = libraryFunction()
if (isSuccess) {
onSuccess()
}
}
suspend fun onSuccess() {
Log.d("Repo", "onSuccess called")
delay(1000)
}
}
Function from a library that can't be modified:
private val someItems = listOf("1", "2", "3")
suspend fun libraryFunction(): Boolean = coroutineScope {
someItems.map { item ->
async {
processItem(item)
}
}.awaitAll()
true
}
suspend fun processItem(item: String) = withContext(Dispatchers.IO) {
delay(5000) // simulate processing
}
When Activity
/Fragment
is closed before onSuccess()
is executed the log
onSuccess called
isn't printed in the Logcat.
In some situations I need to execute onSuccess()
function even if Activity
/Fragment
is closed before.
Is it possible to do it without using GlobalScope
, Service
and WorkManager
classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的问题与Coroutine范围有关。
您正在使用
ViewModelsCope
启动Coroutine:https:> https://developer.androd.androd.android.com/topic/topic/libraries/libraries/coroutine/cortinetine-cororinetine-coroutine-coroutine-coroutine-coroutentin-coroutine-coroutetine,# ViewModelScope
您可以使用
GlobalsCope
或其他其他。有关Coroutine范围的详细信息,您可以在此处找到:
The issue you described, related to the coroutine scope.
You are using
ViewModelScope
when launching coroutine:https://developer.android.com/topic/libraries/architecture/coroutines#viewmodelscope
You can use
GlobalScope
or some other.Details about coroutine's scope you can find here:
https://www.geeksforgeeks.org/scopes-in-kotlin-coroutines/