This is a Kotlin Android library to create async background tasks. Inspired by TinyTask, but more beautiful and easy to use for Kotlin Android developments.
Android's AsyncTasks
are highly criticized for being bad, unreliable, outdated, etc. Are they perfect? No.
Do we have better alternatives? Sure, but sometimes all we want is a quick and simple way to run something in the background.
Just a wrapper around an AsyncTask
, with a funny looking API.
The main difference between KillerTask and TinyTask is that this library is written in Kotlin language, very different from Java.
To learn how to use Kotlin in your Android app you can visit: Android development with Kotlin
Add the following to your build.gradle
file:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
// ...
compile 'com.github.inaka:killertask:v1.2'
// ...
}
val onSuccess: (String) -> Unit = {
result: String ->
Log.wtf("result", result)
}
val onFailed: (Exception?) -> Unit = {
e: Exception? ->
Log.wtf("result", e.toString())
}
val doWork: () -> String = {
"test" // implicit return
}
var killerTask = KillerTask(doWork, onSuccess, onFailed)
killerTask.go() // to execute it
killerTask.cancel() // to cancel it
Or simply you can do:
KillerTask(
{ "test" }, // task
{result: String -> Log.wtf("result", result)}, // onSuccess actions
{e: Exception? -> Log.wtf("result", e.toString())} // onFailed actions
).go()
Actually, the only strongly necessary parameter is the first one (the main task):
KillerTask({ Log.wtf("LOG", "KillerTask is awesome") }).go() // only main task
KillerTask(
{ Log.wtf("LOG", "KillerTask is awesome") }, // main task
{ Log.wtf("LOG", "Super awesome!")} // onSuccess
).go()
KillerTask(
{ // main task
Log.wtf("LOG", "KillerTask is awesome")
"super" // implicit return
},
{}, // onSuccess is empty
{ e: Exception? -> Log.wtf("LOG", e.toString()) } // onFailed
).go()
If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request :)).