Enroscar Async
Makes it easy to put your asynchronous operations behind Android's Loader.
Main abstraction here is Async
which represents some asynchronous operation.
How it is used
Read our recipes page. Here is a quick overview.
Describe an asynchronous operation.
class Foo {
@Load Async<String> loadGreeting(final String name) {
return Tools.async(new Callable<String>() {
public String call() {
try { Thread.sleep(1000); } catch (InterruptedException ignored) { }
return "Hello " + name;
}
});
}
}
In this example we create a simple Callable
and pass it to Tools.async
method that constructs an Async
implementation running our Callable
in Android's AsyncTask
.
An annotations processor generates an operator class FooOperator
that can be used to start/cancel your operations or subscribe to their execution results. Use it!
// this an object that provides operations
Foo foo = new Foo();
// prepare the operator
// FooOperator is a generated class
FooOperator operator = FooOperator.build()
.withinActivity(activity)
.operations(foo)
.get();
// subscribing
operator.when().loadGreetingIsFinished()
.doOnResult(new Action<String>() {
@Override
public void act(final String greeting) {
Log.i("Async", "Greeting loaded: " + greeting);
}
});
// starting
operator.loadGreeting();
// cancelling
operator.cancelLoadGreeting();
Note that working with an operator
you actually control an Android Loader
. Hence, using it within an Activity
or Fragment
you do not care about their lifecycle. Actions you provide subscribing to operations will be automatically attached to running tasks during Activity
recreation.
Download
Gradle
compile 'com.stanfy.enroscar:enroscar-async:{latestVersionHere}'
// annotation processor should be added to 'provided' configuration
// it will not be included to your apk
provided 'com.stanfy.enroscar:enroscar-async-compiler:{latestVersionHere}'
To use it with RxJava you'll also need
compile 'com.stanfy.enroscar:enroscar-async-rx:{latestVersionHere}'
compile 'com.netflix.rxjava:rxjava-core:{latestVersionHere}'
License
Copyright 2011-2015 Stanfy Corp.
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.