From a71c67b9134f8ef1798a9bf0d1c7ff0fff9bd8c4 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Fri, 12 Jul 2019 18:45:25 +0800 Subject: [PATCH] update --- .../legado/app/ui/search/SearchViewModel.kt | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 4d32fb49d..4dc615062 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -9,47 +9,41 @@ import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.entities.SearchBook import io.legado.app.help.http.HttpHelper import kotlinx.coroutines.delay -import kotlinx.coroutines.launch - class SearchViewModel(application: Application) : BaseViewModel(application) { val searchBooks: LiveData> = MutableLiveData() fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { - launch { - delay(1000L) - - - repeat(100) { - test(it) - } - - } - } - - - private fun test(index: Int) { - submit { - val response = HttpHelper.getApiService( + execute { + val response: String = HttpHelper.getApiService( "http://www.baidu.com" - ).get("http://www.baidu.com") + ).get("http://www.baidu.com").await() - Log.e("TAG", "next: $index") + delay(4000L) + + Log.e("TAG1", Thread.currentThread().name) response + } + .timeout { 100L } + .onErrorReturn { "error return" } .onStart { - Log.e("TAG!", "start: $index") + Log.e("TAG!", "start") + start?.let { it() } } .onSuccess { - Log.e("TAG!", "success: $index --> $it") + Log.e("TAG!", "success: $it") } .onError { - Log.e("TAG!", "error: $index --> $it") + Log.e("TAG!", "error: $it") } .onFinally { Log.e("TAG!", "finally") + if (finally != null) { + finally() + } } }