AsyncTask Plus
1、More concurrency control
2、Support Priority
3、Support Task grouping
4、Support lifecycle
5、Support chain invocation
- Initialization(optional)
LogProxy.init(object : TaskLogger {
override val isDebug: Boolean
get() = BuildConfig.DEBUG
override fun e(tag: String, e: Throwable) {
Log.e(tag, e.message, e)
}
})
- Notify Lifecycle Events
abstract class BaseActivity : Activity() {
override fun onDestroy() {
super.onDestroy()
LifecycleManager.notify(this, LifeEvent.DESTROY)
}
override fun onPause() {
super.onPause()
LifecycleManager.notify(this, LifeEvent.HIDE)
}
override fun onResume() {
super.onResume()
LifecycleManager.notify(this, LifeEvent.SHOW)
}
}
Just like AsyncTask:
override fun onCreate(savedInstanceState: Bundle?) {
// ...
TestTask()
.priority(Priority.IMMEDIATE)
.host(this)
.execute("hello")
}
private inner class TestTask: UITask<String, Integer, String>(){
override fun generateTag(): String {
// Normally, you don't need to override this function
return "custom tag"
}
override fun onPreExecute() {
result_tv.text = "running"
}
override fun doInBackground(vararg params: String): String? {
for (i in 0..100 step 2) {
Thread.sleep(10)
publishProgress(Integer(i))
}
return "result is:" + params[0].toUpperCase()
}
override fun onProgressUpdate(vararg values: Integer) {
val progress = values[0]
progress_bar.progress = progress.toInt()
progress_tv.text = "$progress%"
}
override fun onPostExecute(result: String?) {
result_tv.text = result
}
override fun onCancelled() {
showTips("Task cancel ")
}
}
TaskCenter.io.execute{
// do something
}
TaskCenter.laneIO.execute("tag", {
// do something
})
val serialExecutor = PipeExecutor(1)
serialExecutor.execute{
// do something
}
TaskCenter.serial.execute ("your tag", {
// do something
})
object TaskSchedulers {
val io: Scheduler by lazy { Schedulers.from(TaskCenter.io) }
val computation: Scheduler by lazy { Schedulers.from(TaskCenter.computation) }
val single by lazy { Schedulers.from(PipeExecutor(1)) }
}
Observable.range(1, 8)
.subscribeOn(TaskSchedulers.computation)
.subscribe { Log.d(tag, "number:$it") }
override fun onCreate(savedInstanceState: Bundle?) {
val task = ChainTask<Double, Int, String>()
task.tag("ChainTest")
.preExecute { result_tv.text = "running" }
.background { params ->
for (i in 0..100 step 2) {
// do something
task.publishProgress(i)
}
"result is:" + (params[0] * 100)
}
.progressUpdate { values ->
val progress = values[0]
progress_bar.progress = progress
progress_tv.text = "$progress%"
}
.postExecute { result_tv.text = it }
.cancel { showTips("ChainTask cancel") }
.priority(Priority.IMMEDIATE)
.host(this)
.execute(3.14)
}
https://www.jianshu.com/p/8afb6cf64eec
See the LICENSE file for license rights and limitations.