diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index e9bae2236..98e09354b 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -2,6 +2,7 @@ package io.legado.app.base import android.app.Application import android.content.Context +import androidx.annotation.CallSuper import androidx.lifecycle.AndroidViewModel import io.legado.app.App import io.legado.app.help.coroutine.Coroutine @@ -31,6 +32,7 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio return Coroutine.async(scope) { block().await() } } + @CallSuper override fun onCleared() { super.onCleared() cancel() diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index db4a5e889..cd67d50ec 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -12,11 +12,14 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.delay import kotlinx.coroutines.withContext import org.jetbrains.anko.debug +import java.util.concurrent.Executors class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { + private var searchPool = Executors.newFixedThreadPool(99).asCoroutineDispatcher() var callBack: CallBack? = null val searchStateData = MutableLiveData() var name: String = "" @@ -51,7 +54,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } fun search() { - task = execute { + task = execute(context = searchPool) { searchStateData.postValue(true) val bookSourceList = App.db.bookSourceDao().allEnabled for (item in bookSourceList) { @@ -130,4 +133,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio interface CallBack { fun adapter(): ChangeSourceAdapter } + + override fun onCleared() { + searchPool.close() + super.onCleared() + } } \ No newline at end of file