Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/inaka/KillerTask

Android AsyncTask wrapper library, written in Kotlin
https://github.com/inaka/KillerTask

android async kotlin task

Last synced: about 2 months ago
JSON representation

Android AsyncTask wrapper library, written in Kotlin

Awesome Lists containing this project

README

        

KillerTask
=====

This is a Kotlin Android library to create async background tasks. Inspired by [TinyTask](https://github.com/inaka/TinyTask), but more beautiful and easy to use for Kotlin Android developments.

### Abstract
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.

### What is it, really?
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](http://inaka.net/blog/2016/01/15/android-development-with-kotlin)

### How to download and install it
Add the following to your `build.gradle` file:

```groovy
repositories {
maven {
url "https://jitpack.io"
}
}

dependencies {
// ...
compile 'com.github.inaka:killertask:v1.2'
// ...
}
```

### Code examples

```kotlin
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)
```
```kotlin
killerTask.go() // to execute it
```
```kotlin
killerTask.cancel() // to cancel it
```

Or simply you can do:

```kotlin
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):

```kotlin
KillerTask({ Log.wtf("LOG", "KillerTask is awesome") }).go() // only main task
```
```kotlin
KillerTask(
{ Log.wtf("LOG", "KillerTask is awesome") }, // main task
{ Log.wtf("LOG", "Super awesome!")} // onSuccess
).go()
```
```kotlin
KillerTask(
{ // main task
Log.wtf("LOG", "KillerTask is awesome")
"super" // implicit return
},
{}, // onSuccess is empty
{ e: Exception? -> Log.wtf("LOG", e.toString()) } // onFailed
).go()
```

### Example of an app using KillerTask
* [Kotlillon](https://github.com/inaka/kotlillon)

### Contact Us
If you find any **bugs** or have a **problem** while using this library, please [open an issue](https://github.com/inaka/KillerTask/issues/new) in this repo (or a pull request :)).