Package com.bumptech.glide.request
Class RequestFutureTarget<R>
- java.lang.Object
-
- com.bumptech.glide.request.RequestFutureTarget<R>
-
- Type Parameters:
R- The type of the resource that will be loaded.
- All Implemented Interfaces:
LifecycleListener,FutureTarget<R>,RequestListener<R>,Target<R>,java.util.concurrent.Future<R>
public class RequestFutureTarget<R> extends java.lang.Object implements FutureTarget<R>, RequestListener<R>
AFutureimplementation 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:
TheFutureTarget<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 } }cancel(boolean)call will cancel pending operations and make sure that any resources used are recycled.
-
-
Field Summary
-
Fields inherited from interface com.bumptech.glide.request.target.Target
SIZE_ORIGINAL
-
-
Constructor Summary
Constructors Constructor Description RequestFutureTarget(int width, int height)Constructor for a RequestFutureTarget.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancel(boolean mayInterruptIfRunning)Rget()Rget(long time, java.util.concurrent.TimeUnit timeUnit)RequestgetRequest()Retrieves the current request for this target, should not be called outside of Glide.voidgetSize(SizeReadyCallback cb)A callback that should never be invoked directly.booleanisCancelled()booleanisDone()voidonDestroy()Callback for whenFragment.onDestroy()} orActivity.onDestroy()is called.voidonLoadCleared(android.graphics.drawable.Drawable placeholder)A callback that should never be invoked directly.voidonLoadFailed(android.graphics.drawable.Drawable errorDrawable)A callback that should never be invoked directly.booleanonLoadFailed(GlideException e, java.lang.Object model, Target<R> target, boolean isFirstResource)Called when an exception occurs during a load, immediately beforeTarget.onLoadFailed(Drawable).voidonLoadStarted(android.graphics.drawable.Drawable placeholder)A callback that should never be invoked directly.voidonResourceReady(R resource, Transition<? super R> transition)A callback that should never be invoked directly.booleanonResourceReady(R resource, java.lang.Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)Called when a load completes successfully, immediately beforeTarget.onResourceReady(Object, com.bumptech.glide.request.transition.Transition).voidonStart()Callback for whenFragment.onStart()} orActivity.onStart()is called.voidonStop()Callback for whenFragment.onStop()} orActivity.onStop()} is called.voidremoveCallback(SizeReadyCallback cb)Removes the given callback from the pending set if it's still retained.voidsetRequest(Request request)Sets the current request for this target to retain, should not be called outside of Glide.java.lang.StringtoString()
-
-
-
Method Detail
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
- Specified by:
cancelin interfacejava.util.concurrent.Future<R>
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfacejava.util.concurrent.Future<R>
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<R>
-
get
public R get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
- Specified by:
getin interfacejava.util.concurrent.Future<R>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionException
-
get
public R get(long time, @NonNull java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
- Specified by:
getin interfacejava.util.concurrent.Future<R>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
getSize
public void getSize(@NonNull SizeReadyCallback cb)A callback that should never be invoked directly.
-
removeCallback
public void removeCallback(@NonNull SizeReadyCallback cb)Description copied from interface:TargetRemoves the given callback from the pending set if it's still retained.- Specified by:
removeCallbackin interfaceTarget<R>- Parameters:
cb- The callback to remove.
-
setRequest
public void setRequest(@Nullable Request request)Description copied from interface:TargetSets the current request for this target to retain, should not be called outside of Glide.- Specified by:
setRequestin interfaceTarget<R>
-
getRequest
@Nullable public Request getRequest()
Description copied from interface:TargetRetrieves the current request for this target, should not be called outside of Glide.- Specified by:
getRequestin interfaceTarget<R>
-
onLoadCleared
public void onLoadCleared(@Nullable android.graphics.drawable.Drawable placeholder)A callback that should never be invoked directly.- Specified by:
onLoadClearedin interfaceTarget<R>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
onLoadStarted
public void onLoadStarted(@Nullable android.graphics.drawable.Drawable placeholder)A callback that should never be invoked directly.- Specified by:
onLoadStartedin interfaceTarget<R>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
onLoadFailed
public void onLoadFailed(@Nullable android.graphics.drawable.Drawable errorDrawable)A callback that should never be invoked directly.- Specified by:
onLoadFailedin interfaceTarget<R>- Parameters:
errorDrawable- The error drawable to optionally show, or null.
-
onResourceReady
public void onResourceReady(@NonNull R resource, @Nullable Transition<? super R> transition)A callback that should never be invoked directly.- Specified by:
onResourceReadyin interfaceTarget<R>- Parameters:
resource- the loaded resource.
-
onStart
public void onStart()
Description copied from interface:LifecycleListenerCallback for whenFragment.onStart()} orActivity.onStart()is called.- Specified by:
onStartin interfaceLifecycleListener
-
onStop
public void onStop()
Description copied from interface:LifecycleListenerCallback for whenFragment.onStop()} orActivity.onStop()} is called.- Specified by:
onStopin interfaceLifecycleListener
-
onDestroy
public void onDestroy()
Description copied from interface:LifecycleListenerCallback for whenFragment.onDestroy()} orActivity.onDestroy()is called.- Specified by:
onDestroyin interfaceLifecycleListener
-
onLoadFailed
public boolean onLoadFailed(@Nullable GlideException e, java.lang.Object model, Target<R> target, boolean isFirstResource)Description copied from interface:RequestListenerCalled when an exception occurs during a load, immediately beforeTarget.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 toGlide.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
Targetat this point, as long as you returntruefrom the method to preventTarget.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. }- Specified by:
onLoadFailedin interfaceRequestListener<R>- Parameters:
e- The maybenullexception containing information about why the request failed.model- The model we were trying to load when the exception occurred.target- TheTargetwe were trying to load the image into.isFirstResource-trueif this exception is for the first resource to load.- Returns:
trueto preventTarget.onLoadFailed(Drawable)from being called ontarget, typically because the listener wants to update thetargetor the object thetargetwraps itself orfalseto allowTarget.onLoadFailed(Drawable)to be called ontarget.
-
onResourceReady
public boolean onResourceReady(R resource, java.lang.Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)
Description copied from interface:RequestListenerCalled when a load completes successfully, immediately beforeTarget.onResourceReady(Object, com.bumptech.glide.request.transition.Transition).For threading guarantees, see the class comment.
- Specified by:
onResourceReadyin interfaceRequestListener<R>- Parameters:
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- TheDataSourcethe resource was loaded from.isFirstResource-trueif 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 betruefor the first image to load andfalsefor the second.- Returns:
trueto preventTarget.onResourceReady(Object, Transition)from being called ontarget, typically because the listener wants to update thetargetor the object thetargetwraps itself orfalseto allowTarget.onResourceReady(Object, Transition)to be called ontarget.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-