public class JRoutine extends JRoutine
equals() and hashCode() methods of the input parameter objects and the
invocation factory, might be employed to check for clashing of invocation instances or compute
the loader ID.InvocationTypeException.
For example, in order to get a resource from the network, needed to fill an activity UI:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
if (savedInstanceState != null) {
mResource = savedInstanceState.getParcelable(RESOURCE_KEY);
}
if (mResource != null) {
displayResource(mResource);
} else {
final Routine<URI, MyResource> routine =
JRoutine.on(contextFrom(this), factoryOf(LoadResource.class))
.buildRoutine();
routine.asyncCall(RESOURCE_URI)
.passTo(new TemplateOutputConsumer<MyResource>() {
@Override
public void onError(@Nullable final Throwable error) {
displayError(error);
}
@Override
public void onOutput(final MyResource resource) {
mResource = resource;
displayResource(resource);
}
});
}
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(RESOURCE_KEY, mResource);
}
The above code will ensure that the loading process survives any configuration change and the
resulting resource is dispatched only once.
Note that the invocation may be implemented so to run in a separate service:
public class LoadResource extends TemplateContextInvocation<URI, MyResource> {
private Routine<URI, MyResource> mRoutine;
@Override
public void onContext(@Nonnull final Context context) {
super.onContext(context);
mRoutine =
JRoutine.on(serviceFrom(context), targetInvocation(LoadResourceUri.class))
.buildRoutine();
}
@Override
public void onInput(final URI uri,
@Nonnull final ResultChannel<MyResource> result) {
result.pass(mRoutine.asyncCall(uri));
}
}
Created by davide-maestroni on 12/08/2014.| Constructor and Description |
|---|
JRoutine() |
| Modifier and Type | Method and Description |
|---|---|
static LoaderChannelBuilder |
on(LoaderContext context)
Returns a builder of an output channel bound to the loader identified by the ID specified in
the loader configuration.
If no invocation with the specified ID is running at the time of the channel creation, the output will be aborted with a MissingInvocationException.Note that the built routine results will be always dispatched on the configured looper thread, thus waiting for the outputs immediately after its invocation may result in a deadlock. |
static <IN,OUT> LoaderRoutineBuilder<IN,OUT> |
on(LoaderContext context,
ContextInvocationFactory<IN,OUT> factory)
Returns a builder of routines bound to the specified context.
In order to prevent undesired leaks, the class of the specified factory must be static, and should never be a platform component (like Activity, Fragment, etc.). Note that the built routine results will be always dispatched on the configured looper thread, thus waiting for the outputs immediately after its invocation may result in a deadlock. |
static LoaderObjectRoutineBuilder |
on(LoaderContext context,
ContextInvocationTarget target)
Returns a builder of routines bound to the specified context, wrapping the specified target
object.
In order to customize the object creation, the caller must employ an implementation of a FactoryContext as the application
context.Note that the built routine results will be always dispatched on the configured looper thread, thus waiting for the outputs immediately after its invocation may result in a deadlock. |
@Nonnull public static LoaderChannelBuilder on(@Nonnull LoaderContext context)
MissingInvocationException.context - the routine context.@Nonnull public static <IN,OUT> LoaderRoutineBuilder<IN,OUT> on(@Nonnull LoaderContext context, @Nonnull ContextInvocationFactory<IN,OUT> factory)
IN - the input data type.OUT - the output data type.context - the routine context.factory - the invocation factory.IllegalArgumentException - if the class of the specified factory is not
static.@Nonnull public static LoaderObjectRoutineBuilder on(@Nonnull LoaderContext context, @Nonnull ContextInvocationTarget target)
FactoryContext as the application
context.context - the routine context.target - the invocation target.