forked from GitLiveApp/firebase-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncTask.kt
More file actions
34 lines (27 loc) · 867 Bytes
/
Copy pathAsyncTask.kt
File metadata and controls
34 lines (27 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package android.os
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.Executor
abstract class AsyncTask {
companion object {
@JvmField
val THREAD_POOL_EXECUTOR = Executor {
GlobalScope.launch {
it.run()
}
}
}
fun execute(vararg params: Any): AsyncTask {
GlobalScope.launch {
withContext(Dispatchers.Main) { onPreExecute() }
val result = doInBackground(*params)
withContext(Dispatchers.Main) { onPostExecute(result) }
}
return this
}
protected abstract fun doInBackground(vararg params: Any): Any
protected open fun onPreExecute() {}
protected open fun onPostExecute(result: Any) {}
}