Java 异步任务在Kotlin中的使用
我对Kotlin中的AsyncTask有一个问题,我实际上是新手,所以请冷静:) 这里的问题是,我想在另一个类中使用onPostExecute()中的结果值 让我向您展示我的代码,在我的Provider.kt文件中有一个ProviderAsync()类(我只是创建了一个要使用的Hashmap数组):Java 异步任务在Kotlin中的使用,java,android,asynchronous,android-asynctask,kotlin,Java,Android,Asynchronous,Android Asynctask,Kotlin,我对Kotlin中的AsyncTask有一个问题,我实际上是新手,所以请冷静:) 这里的问题是,我想在另一个类中使用onPostExecute()中的结果值 让我向您展示我的代码,在我的Provider.kt文件中有一个ProviderAsync()类(我只是创建了一个要使用的Hashmap数组): 感谢并抱歉我的英语您可以利用Kotlin的功能性: class ProviderAsync(private val callback: (things: ArrayList<HashMap&l
感谢并抱歉我的英语您可以利用Kotlin的功能性:
class ProviderAsync(private val callback: (things: ArrayList<HashMap<String, Any>>) -> Unit) : AsyncTask<HashMap<String, Any>, Void, ArrayList<HashMap<String, Any>>>() {
var allThings: ArrayList<HashMap<String, Any>> = arrayListOf()
override fun doInBackground(vararg params: HashMap<String, Any>): ArrayList<HashMap<String, Any>>? {
for (i in 0..2000) {
val thing = hashMapOf("Loc" to "fr", "name" to "class", "Id" to "23", "tuto" to "fr", "price" to 44)
allThings.add(thing)
}
return null
}
override fun onPreExecute() {
super.onPreExecute()
// ...
}
override fun onPostExecute(result: ArrayList<HashMap<String, Any>>) {
super.onPostExecute(result)
//callback function to be executed after getting the result
callback(result)
}
}
截至2020年6月,
AsyncTask
已弃用。这并不意味着该课程将很快被删除,这意味着谷歌建议你转到其他地方。标准java.util.concurrent
或相反
使用最后一个,我们可以按如下方式实现它:
CoroutineScope
上创建通用扩展函数:
fun <R> CoroutineScope.executeAsyncTask(
onPreExecute: () -> Unit,
doInBackground: () -> R,
onPostExecute: (R) -> Unit
) = launch {
onPreExecute()
val result = withContext(Dispatchers.IO) { // runs in background thread without blocking the Main Thread
doInBackground()
}
onPostExecute(result)
}
活动
或片段
中:
lifecycleScope.executeAsyncTask(onPreExecute = {
// ... runs in Main Thread
}, doInBackground = {
// ... runs in Worker(Background) Thread
"Result" // send data to "onPostExecute"
}, onPostExecute = {
// runs in Main Thread
// ... here "it" is the data returned from "doInBackground"
})
viewModelScope
或lifecycleScope
将下一行添加到应用程序的build.gradle文件的依赖项中:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$LIFECYCLE_VERSION" // for viewModelScope
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$LIFECYCLE_VERSION" // for lifecycleScope
在编写final LIFECYCLE\u VERSION=“2.3.0-alpha05”
首先,让我知道为什么您不在想要使用返回值的同一个类中编写异步任务?这是我的体系结构,我希望我的提供程序执行异步任务并将数据发送给我的ManagerRefer。您为什么使用
HashMap
而不是类
?
fun <R> CoroutineScope.executeAsyncTask(
onPreExecute: () -> Unit,
doInBackground: () -> R,
onPostExecute: (R) -> Unit
) = launch {
onPreExecute()
val result = withContext(Dispatchers.IO) { // runs in background thread without blocking the Main Thread
doInBackground()
}
onPostExecute(result)
}
class MyViewModel : ViewModel() {
fun someFun() {
viewModelScope.executeAsyncTask(onPreExecute = {
// ... runs in Main Thread
}, doInBackground = {
// ... runs in Worker(Background) Thread
"Result" // send data to "onPostExecute"
}, onPostExecute = {
// runs in Main Thread
// ... here "it" is the data returned from "doInBackground"
})
}
}
lifecycleScope.executeAsyncTask(onPreExecute = {
// ... runs in Main Thread
}, doInBackground = {
// ... runs in Worker(Background) Thread
"Result" // send data to "onPostExecute"
}, onPostExecute = {
// runs in Main Thread
// ... here "it" is the data returned from "doInBackground"
})
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$LIFECYCLE_VERSION" // for viewModelScope
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$LIFECYCLE_VERSION" // for lifecycleScope