public final class GlideFutures
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
GlideFutures.ResourceConsumer<T>
Acts on a resource loaded by Glide.
|
Modifier and Type | Method and Description |
---|---|
static <T> com.google.common.util.concurrent.ListenableFuture<T> |
submit(RequestBuilder<T> requestBuilder)
Convert a pending load request into a ListenableFuture.
|
static <T> com.google.common.util.concurrent.ListenableFuture<java.lang.Void> |
submitAndExecute(RequestManager requestManager,
RequestBuilder<T> requestBuilder,
GlideFutures.ResourceConsumer<T> action,
java.util.concurrent.Executor executor)
Submits the provided request, performs the provided
action and returns a ListenableFuture that can be used to cancel the request or monitor its status. |
public static <T> com.google.common.util.concurrent.ListenableFuture<java.lang.Void> submitAndExecute(RequestManager requestManager, RequestBuilder<T> requestBuilder, GlideFutures.ResourceConsumer<T> action, java.util.concurrent.Executor executor)
action
and returns a ListenableFuture
that can be used to cancel the request or monitor its status.
Cancellation is best effort and may result in some resources not being returned back to
Glide's pool. In particular, if the request is cancelled after the resource is loaded by Glide,
but before action
is run on executor
, the resource will not be returned. We
have the unfortunate choice between unsafely returning resources to the pool immediately when
cancel is called while they may still be in use via com.google.common.util.concurrent.ClosingFuture
or occasionally failing to return resources to
the pool. Because failing to return resources to the pool is inefficient, but safe, that's the
route we've chosen. A more sophisticated implementation may allow us to avoid the resource
inefficiency.
If you do not need to interact with resource, use #preload(RequestManager,
RequestBuilder, Executor)
. preload
is more efficient because it knows that the
resource is never used and can always clear the resource immediately on cancellation, unlike
this method.
An example usage:
ListenableFuture<Void> future =
submitAndExecute(
requestManager,
requestBuilder,
(bitmap) -> doSomethingWithBitmap(bitmap),
backgroundExecutor);
;
T
- The type of resource that will be loaded (Bitmap, Drawable, etc).public static <T> com.google.common.util.concurrent.ListenableFuture<T> submit(RequestBuilder<T> requestBuilder)
Sample code:
ListenableFuture<File> image =
GlideFutures.submit(requestManager.asFile().load(url));
T
- The type of resource that will be loaded (Bitmap, Drawable, etc).requestBuilder
- A request builder for the resource to load. It must be tied to an
application Glide instance, and must not have a listener set.