inaka

Latest blog entries

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Mar 09 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
See all Inaka's blog posts >>

/
KillerTask, the solution to AsyncTask implementation

Fernando Ramirez wrote this on January 25, 2016 under android, asynctask, dev, functional, gradle, inaka, killertask, kotlin, language, library .

Introduction

KillerTask is an Android library to create asynchronous background tasks.

Developed in Kotlin and inspired by TinyTask, this is more beautiful and easy to use for Kotlin Android developments.

Wait... Kotlin?

To learn what Kotlin is, how to use it in Android and their characteristics, you can take a look at: "Android development with Kotlin".

So, is this the same as TinyTask? :/

Well, both are just wrappers around AsyncTask, with a funny looking API. But no, is not exactly the same.

The main difference between KillerTask and TinyTask is that this library is written in Kotlin language, very different from Java. If you use Kotlin for develop your Android apps, you will find this much more clear and reliable.

In addition, KillerTask has a better management of threads compared to TinyTask's actual last version (1.0.1).

Include it in your project

Add the following to your app/build.gradle file:

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

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

How it works

Create a new KillerTask

var killerTask = KillerTask(doWork, onSuccess, onFailed)

Having for example:

// task function
val doWork: () -> String = {
  var connection: URLConnection? = null;

  try {
      var url = URL("https://api.github.com/gists")
      connection = url.openConnection();
  } catch (e: Exception) {
      e.printStackTrace()
  }

  var httpConn = connection as HttpURLConnection;
  httpConn.connectTimeout = 3000;
  httpConn.readTimeout = 5000;

  // implicit return
  httpConn.responseCode.toString() + " " + httpConn.responseMessage
}

// onSuccess function
val onSuccess: (String) -> Unit = {
  result: String ->
  Log.wtf("success result", result)
}

// onFailed function
val onFailed: (Exception?) -> Unit = {
  e: Exception? ->
  Log.wtf("error result", e.toString())
  e?.printStackTrace()
}

To execute it:

killerTask.go()

To cancel it:

killerTask.cancel()

All together

You can write it all together, for example:

KillerTask(
    { "test" }, // main task returning "test"
    {result: String -> Log.wtf("result", result)}, // onSuccess actions
    {e: Exception? -> Log.wtf("result", e.toString())} // onFailed actions
  ).go()

Variable parameters

Actually, the only strongly necessary parameter is the first one (the main task).

  • Example without onSuccess and onFailed actions:
KillerTask({ Log.wtf("LOG", "KillerTask is awesome") }).go() // only main task
  • Example only with onSuccess actions:
KillerTask(
    { Log.wtf("LOG", "KillerTask is awesome") }, // main task
    { Log.wtf("LOG", "Super awesome!")} // onSuccess
  ).go()
  • Example only with on Failed actions:
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 Compilation of simple Kotlin examples, all together in one Android app.

Support

For questions or general comments regarding the use of this library, please use our public hipchat room.

If you find any bugs or have a problem while using this library, please open an issue (or send a pull request with your solution).

You can check all of our open-source projects at inaka.github.io.