R
- The type of the resource that will be loaded.public class RequestFutureTarget<R> extends Object implements FutureTarget<R>, RequestListener<R>, Runnable
Future
implementation for Glide that can be used to load resources
in a blocking manner on background threads.
Note - Unlike most targets, RequestFutureTargets can be used once and only once. Attempting to reuse a RequestFutureTarget will probably result in undesirable behavior or exceptions. Instead of reusing objects of this class, the pattern should be:
FutureTarget<File> target = null;
RequestManager requestManager = Glide.with(context);
try {
target = requestManager
.downloadOnly()
.load(model)
.submit();
File downloadedFile = target.get();
// ... do something with the file (usually throws IOException)
} catch (ExecutionException | InterruptedException | IOException e) {
// ... bug reporting or recovery
} finally {
// make sure to cancel pending operations and free resources
if (target != null) {
target.cancel(true); // mayInterruptIfRunning
}
}
The cancel(boolean)
call will cancel pending operations and
make sure that any resources used are recycled.
SIZE_ORIGINAL
Constructor and Description |
---|
RequestFutureTarget(Handler mainHandler,
int width,
int height)
Constructor for a RequestFutureTarget.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning) |
R |
get() |
R |
get(long time,
TimeUnit timeUnit) |
Request |
getRequest()
Retrieves the current request for this target, should not be called outside of Glide.
|
void |
getSize(SizeReadyCallback cb)
A callback that should never be invoked directly.
|
boolean |
isCancelled() |
boolean |
isDone() |
void |
onDestroy()
Callback for when
Fragment.onDestroy() } or Activity.onDestroy() is called. |
void |
onLoadCleared(Drawable placeholder)
A callback that should never be invoked directly.
|
void |
onLoadFailed(Drawable errorDrawable)
A callback that should never be invoked directly.
|
boolean |
onLoadFailed(GlideException e,
Object model,
Target<R> target,
boolean isFirstResource)
Called when an exception occurs during a load, immediately before
Target.onLoadFailed(Drawable) . |
void |
onLoadStarted(Drawable placeholder)
A callback that should never be invoked directly.
|
boolean |
onResourceReady(R resource,
Object model,
Target<R> target,
DataSource dataSource,
boolean isFirstResource)
Called when a load completes successfully, immediately before
Target.onResourceReady(Object, com.bumptech.glide.request.transition.Transition) . |
void |
onResourceReady(R resource,
Transition<? super R> transition)
A callback that should never be invoked directly.
|
void |
onStart()
Callback for when
Fragment.onStart() } or Activity.onStart() is called. |
void |
onStop()
Callback for when
Fragment.onStop() } or Activity.onStop() } is called. |
void |
removeCallback(SizeReadyCallback cb)
Removes the given callback from the pending set if it's still retained.
|
void |
run()
A callback that should never be invoked directly.
|
void |
setRequest(Request request)
Sets the current request for this target to retain, should not be called outside of Glide.
|
public RequestFutureTarget(Handler mainHandler, int width, int height)
public boolean cancel(boolean mayInterruptIfRunning)
public boolean isCancelled()
isCancelled
in interface Future<R>
public R get() throws InterruptedException, ExecutionException
get
in interface Future<R>
InterruptedException
ExecutionException
public R get(long time, @NonNull TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException
get
in interface Future<R>
InterruptedException
ExecutionException
TimeoutException
public void getSize(@NonNull SizeReadyCallback cb)
public void removeCallback(@NonNull SizeReadyCallback cb)
Target
removeCallback
in interface Target<R>
cb
- The callback to remove.public void setRequest(@Nullable Request request)
Target
setRequest
in interface Target<R>
@Nullable public Request getRequest()
Target
getRequest
in interface Target<R>
public void onLoadCleared(@Nullable Drawable placeholder)
onLoadCleared
in interface Target<R>
placeholder
- The placeholder drawable to optionally show, or null.public void onLoadStarted(@Nullable Drawable placeholder)
onLoadStarted
in interface Target<R>
placeholder
- The placeholder drawable to optionally show, or null.public void onLoadFailed(@Nullable Drawable errorDrawable)
onLoadFailed
in interface Target<R>
errorDrawable
- The error drawable to optionally show, or null.public void onResourceReady(@NonNull R resource, @Nullable Transition<? super R> transition)
onResourceReady
in interface Target<R>
resource
- the loaded resource.public void run()
public void onStart()
LifecycleListener
Fragment.onStart()
} or Activity.onStart()
is called.onStart
in interface LifecycleListener
public void onStop()
LifecycleListener
Fragment.onStop()
} or Activity.onStop()
} is called.onStop
in interface LifecycleListener
public void onDestroy()
LifecycleListener
Fragment.onDestroy()
} or Activity.onDestroy()
is called.onDestroy
in interface LifecycleListener
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<R> target, boolean isFirstResource)
RequestListener
Target.onLoadFailed(Drawable)
. Will only be called if we currently want to display an
image for the given model in the given target. It is recommended to create a single instance
per activity/fragment rather than instantiate a new object for each call to Glide.with(fragment/activity).load()
to avoid object churn.
It is not safe to reload this or a different model in this callback. If you need to do so
use RequestBuilder.error(RequestBuilder)
instead.
Although you can't start an entirely new load, it is safe to change what is displayed in the
Target
at this point, as long as you return true
from the method to prevent
Target.onLoadFailed(Drawable)
from being called.
For example:
public boolean onLoadFailed(Exception e, T model, Target target, boolean isFirstResource) {
target.setPlaceholder(R.drawable.a_specific_error_for_my_exception);
return true; // Prevent onLoadFailed from being called on the Target.
}
onLoadFailed
in interface RequestListener<R>
e
- The maybe null
exception containing information about why the
request failed.model
- The model we were trying to load when the exception occurred.target
- The Target
we were trying to load the image into.isFirstResource
- true
if this exception is for the first resource to load.true
to prevent Target.onLoadFailed(Drawable)
from being called on
target
, typically because the listener wants to update the target
or the object
the target
wraps itself or false
to allow Target.onLoadFailed(Drawable)
to be called on target
.public boolean onResourceReady(R resource, Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)
RequestListener
Target.onResourceReady(Object, com.bumptech.glide.request.transition.Transition)
.onResourceReady
in interface RequestListener<R>
resource
- The resource that was loaded for the target.model
- The specific model that was used to load the image.target
- The target the model was loaded into.dataSource
- The DataSource
the resource was loaded from.isFirstResource
- true
if this is the first resource to in this load to be
loaded into the target. For example when loading a thumbnail and a
full-sized image, this will be true
for the first image to
load and false
for the second.true
to prevent Target.onLoadFailed(Drawable)
from being called on
target
, typically because the listener wants to update the target
or the object
the target
wraps itself or false
to allow Target.onLoadFailed(Drawable)
to be called on target
.