R
- The type of the resource that will be loaded.public class RequestFutureTarget<R> extends java.lang.Object implements FutureTarget<R>, RequestListener<R>
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(int width,
int height)
Constructor for a RequestFutureTarget.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning) |
R |
get() |
R |
get(long time,
java.util.concurrent.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(android.graphics.drawable.Drawable placeholder)
A callback that should never be invoked directly.
|
void |
onLoadFailed(android.graphics.drawable.Drawable errorDrawable)
A callback that should never be invoked directly.
|
boolean |
onLoadFailed(GlideException e,
java.lang.Object model,
Target<R> target,
boolean isFirstResource)
Called when an exception occurs during a load, immediately before
Target.onLoadFailed(Drawable) . |
void |
onLoadStarted(android.graphics.drawable.Drawable placeholder)
A callback that should never be invoked directly.
|
boolean |
onResourceReady(R resource,
java.lang.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 |
setRequest(Request request)
Sets the current request for this target to retain, should not be called outside of Glide.
|
public RequestFutureTarget(int width, int height)
public boolean cancel(boolean mayInterruptIfRunning)
cancel
in interface java.util.concurrent.Future<R>
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<R>
public boolean isDone()
isDone
in interface java.util.concurrent.Future<R>
public R get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
get
in interface java.util.concurrent.Future<R>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
public R get(long time, @NonNull java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
get
in interface java.util.concurrent.Future<R>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.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 android.graphics.drawable.Drawable placeholder)
onLoadCleared
in interface Target<R>
placeholder
- The placeholder drawable to optionally show, or null.public void onLoadStarted(@Nullable android.graphics.drawable.Drawable placeholder)
onLoadStarted
in interface Target<R>
placeholder
- The placeholder drawable to optionally show, or null.public void onLoadFailed(@Nullable android.graphics.drawable.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 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, java.lang.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 threading guarantees, see the class comment.
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, java.lang.Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)
RequestListener
Target.onResourceReady(Object, com.bumptech.glide.request.transition.Transition)
.
For threading guarantees, see the class comment.
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
.