• Stars
    star
    114
  • Rank 307,123 (Top 7 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An easy to use AsyncTask replacement - life cycle aware and leak preventing

Thinr

Download Android Arsenal Build Status License Coverage Status

About

Thinr is a replacement for AsyncTask. It is a tiny library that makes things like the following possible in a leak free and configuration change aware way:

Thinr.task(getContext(), "getQOTD", MainActivity.class, Void.class)
                        .onMain((target, param) -> {
                            target.textView.setText(target.getApplicationContext().getString(R.string.loading_message));
                            return null;
                        })
                        .inBackground(
                                (appCtx, param, flowControl) -> {
                                    OkHttpClient client = new OkHttpClient();

                                    Request request = new Request.Builder()
                                            .url("http://api.icndb.com/jokes/random")
                                            .build();

                                    try {
                                        Response response = client.newCall(request).execute();
                                        JSONObject json = new JSONObject(response.body().string());
                                        return json.getJSONObject("value").getString("joke");
                                    } catch (Exception e) {
                                        return null;
                                    }
                                }
                        )
                        .endsOnMain((target, qotd) -> {
                            if (qotd != null) {
                                target.textView.setText(qotd);
                            } else {
                                target.textView.setText(target.getApplicationContext().getString(R.string.error_message));
                            }
                        })
                        .execute(null, "ComponentID");

While the above example is not very exciting you might be able to estimate it's value considering it's

  • life cycle aware
  • leak free

Anything in "inBackground" will be executing on a background thread.

Anything in "onMain" will be executing when - and only when - the component is in a state that is fine for accessing the component. Imagine that "inBackground" can be any time consuming blocking operation accessing the network and hitting the filesystem.

It's a beautiful replacement for AsyncTask (which it internally uses) when using Java 8.

You can also use it with Java 7 but Java 8 will make your experience much more exciting.

Currently the official support for Java 8 is limited to using Jack (see http://developer.android.com/preview/j8-jack.html ) but for sure the complete toolchain will support Java 8 sooner or later.

Additionally RetroLambda is supported starting with version 0.1.0.

If you still don't know what this will do for you have a look at other approaches making AsyncTask work (right) for you.

Don't get me wrong there is nothing wrong with AsyncTask - it just needs a lot of care to implement it in a leak and error free way.

Unfortunately those implementations add a lot of boiler plate code which made a lot of developers to come up with alternative solutions (which are also not easy to understand and do it right - and most of the time the code will look just ugly, hard to understand and hard to maintain).

If you ever did some non-trivial Android app you should know the pain I*m talking about.

Objectives

The objectives of this tiny library are:

  • easy usage
  • easy to understand
  • no boilerplate code involved (when using Java 8)
  • easy to integrate
  • no need to rewrite your whole code base to make use of it / can be introduced step-by-step where it is needed while leaving all the other code untouched
  • non invasive (i.e. don't forces you to use a special style of programming)
  • beautiful code (when using Java 8)

Additionally while the API is quite easy it comes with a lot of flexibility.

Get started

The easiest way to get started is to look at the sample app. It's kept small to focus on the usage of Thinr.

Integrate

This library is available on JCenter Download

Add the dependency to your app and make sure to use the jcenter repository:

    implementation 'de.mobilej:thinr:0.2.7'

Version History

Version Description
0.0.0 initial public release
0.1.0 supports RetroLambda, possible to disable runtime checks
0.1.1 runtime checks now allow RetroLambda and Jacoco at the same time
0.2.0 fixed problem with InstantRun, added a Lint check, added more JavaDoc
0.2.1 whitelist serialVersionUID as a safe field for Lambdas
0.2.2 whitelist serialVersionUID as a safe field for Lambdas
0.2.3 Improved Lint check
0.2.4 Improved Lint check (less false positives)
0.2.5 You can specify the executor for background operations now
0.2.6 No problems with Android Gradle plugin 3.0.0
0.2.7 No problems with Android Gradle plugin 3.2.0

License

Copyright 2016,2017 Björn Quentin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

unmock-plugin

Gradle plugin to be used in combination with the new unit testing feature of the Gradle Plugin / Android Studio to use real classes for e.g. SparseArray.
Java
317
star
2

esp32-rust-nostd-temperature-logger

MQTT temperature logger running on ESP32 in Rust (no-std / no RTOS)
Rust
36
star
3

adc

An Android Studio plugin to control your development devices and emulators. Currently you can do the following from within your IDE:
Java
25
star
4

bl602-wifi-rs

WIP / POC for using the BL602 wifi blob from Rust for wifi and BLE connectivity
Rust
23
star
5

esp32c3-ota-experiment

ESP32-C3 Bare Metal OTA Experiment
Rust
22
star
6

svd2html

A simple script (in Rust lang) to create HTML from SVD
Rust
15
star
7

bleps

A toy-level BLE peripheral stack
Rust
13
star
8

esp32c3-rust-std-temperature-logger

MQTT temperature logger running on ESP32C3 in Rust (std)
Rust
12
star
9

rust_azure_sphere_advanced

Sample using Rust on Azure Sphere with Networking and Multi-Threading
Rust
11
star
10

blash

A tool to flash BL602 via JTAG + serial without pressing buttons / setting jumpers
Rust
10
star
11

bluepet

A PET 2001 emulator on STM32F103 (BluePill)
Rust
9
star
12

bare-matter

Rust
8
star
13

air602_rust

A basic hello world app for the Air602 wifi module written in Rust
Python
6
star
14

esp-bare-matter

Rust
5
star
15

ps2keyboard-esp32c3

ESP32-C3 interfacing to a PS/2 Keyboard (bare-metal Rust)
Rust
4
star
16

esp32c3-sleep-experiment

Experimenting with sleep on ESP32-C3 in bare-metal Rust
Rust
4
star
17

whitemagic

An easy way to do lifecycle aware async task style thing on Android with Kotlin
Kotlin
3
star
18

matter-rs-experiment

Rust
3
star
19

mksettings

Rust
3
star
20

mini-rv32ima-rs

Simple Rust port of mini-rv32ima
Rust
2
star
21

rust_azure_sphere_blinky

Using Rust to develop Blinky (the "Hello World" of embedded) for Azure Sphere
Rust
2
star
22

ssize

Show function's stack-size
Rust
2
star
23

measure-current

INA219 and ESP32-C3 / bare-metal Rust
Rust
2
star
24

esp32-ulp-rust-macro

Rust
2
star
25

puny-tls

A no-std / no-alloc TLS 1.3 client
Rust
2
star
26

kmockit

Kotlin friendly wrapper for JMockit
Kotlin
2
star
27

espsegs

Rust
2
star
28

rconfig

Rust
2
star
29

IntelliJJUnitPatcher

Android Studio Plugin to enable you running standard Java-JUnit tests in the JVM (using robolectric-gradle-plugin)
Java
2
star
30

twatch-bare-metal

Rust
1
star
31

i2c-dummy-device

ESP32-C3 as I2C dummy device
Rust
1
star
32

ble-hci

Work in progress minimal BLE peripheral stack
Rust
1
star
33

esp-hal-get-started

mdBook: Getting Started with bare-metal Rust on ESP32
1
star
34

esp32c3-preemt-experiment

Rust
1
star
35

ps2keyboard-esp32

ESP32 interfacing to a PS/2 Keyboard (bare-metal Rust)
Rust
1
star
36

esp32c3-dhcp-experiment

ESP32-C3 bare-metal captive portal example / experiment
Rust
1
star
37

rust-esp32s3-ili9341

Rust
1
star
38

s3box-sound

Rust
1
star
39

bleps-esp32-rust-std

Using bleps BLE stack on ESP32 in Rust STD
Rust
1
star