提交 6fb87b35 编写于 作者: S Sam Judd

Assert that into() and clear() are not called in callbacks.

Fixes #2413.
上级 1b4a99a7
......@@ -45,6 +45,7 @@ public final class SingleRequest<R> implements Request,
return new SingleRequest<Object>();
}
});
private boolean isCallingCallbacks;
private enum Status {
/**
......@@ -182,6 +183,7 @@ public final class SingleRequest<R> implements Request,
@Override
public void recycle() {
assertNotCallingCallbacks();
glideContext = null;
model = null;
transcodeClass = null;
......@@ -203,6 +205,7 @@ public final class SingleRequest<R> implements Request,
@Override
public void begin() {
assertNotCallingCallbacks();
stateVerifier.throwIfRecycled();
startTime = LogTime.getLogTime();
if (model == null) {
......@@ -260,6 +263,7 @@ public final class SingleRequest<R> implements Request,
* @see #clear()
*/
void cancel() {
assertNotCallingCallbacks();
stateVerifier.throwIfRecycled();
target.removeCallback(this);
status = Status.CANCELLED;
......@@ -269,6 +273,15 @@ public final class SingleRequest<R> implements Request,
}
}
// Avoids difficult to understand errors like #2413.
private void assertNotCallingCallbacks() {
if (isCallingCallbacks) {
throw new IllegalStateException("You can't start or clear loads in RequestListener or"
+ " Target callbacks. If you must do so, consider posting your into() or clear() calls"
+ " to the main thread using a Handler instead.");
}
}
/**
* Cancels the current load if it is in progress, clears any resources held onto by the request
* and replaces the loaded resource if the load completed with the placeholder.
......@@ -280,6 +293,7 @@ public final class SingleRequest<R> implements Request,
@Override
public void clear() {
Util.assertMainThread();
assertNotCallingCallbacks();
if (status == Status.CLEARED) {
return;
}
......@@ -535,11 +549,16 @@ public final class SingleRequest<R> implements Request,
+ LogTime.getElapsedMillis(startTime) + " ms");
}
if (requestListener == null
|| !requestListener.onResourceReady(result, model, target, dataSource, isFirstResource)) {
Transition<? super R> animation =
animationFactory.build(dataSource, isFirstResource);
target.onResourceReady(result, animation);
isCallingCallbacks = true;
try {
if (requestListener == null
|| !requestListener.onResourceReady(result, model, target, dataSource, isFirstResource)) {
Transition<? super R> animation =
animationFactory.build(dataSource, isFirstResource);
target.onResourceReady(result, animation);
}
} finally {
isCallingCallbacks = false;
}
notifyLoadSuccess();
......@@ -565,10 +584,16 @@ public final class SingleRequest<R> implements Request,
loadStatus = null;
status = Status.FAILED;
//TODO: what if this is a thumbnail request?
if (requestListener == null
|| !requestListener.onLoadFailed(e, model, target, isFirstReadyResource())) {
setErrorPlaceholder();
isCallingCallbacks = true;
try {
//TODO: what if this is a thumbnail request?
if (requestListener == null
|| !requestListener.onLoadFailed(e, model, target, isFirstReadyResource())) {
setErrorPlaceholder();
}
} finally {
isCallingCallbacks = false;
}
}
......
package com.bumptech.glide.request.target;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.transition.Transition;
......@@ -10,6 +14,17 @@ import com.bumptech.glide.request.transition.Transition;
* @param <Z> The type of resource that will be loaded into memory.
*/
public final class PreloadTarget<Z> extends SimpleTarget<Z> {
private static final int MESSAGE_CLEAR = 1;
private static final Handler HANDLER = new Handler(Looper.getMainLooper(), new Callback() {
@Override
public boolean handleMessage(Message message) {
if (message.what == MESSAGE_CLEAR) {
((PreloadTarget<?>) message.obj).clear();
return true;
}
return false;
}
});
private final RequestManager requestManager;
......@@ -31,6 +46,10 @@ public final class PreloadTarget<Z> extends SimpleTarget<Z> {
@Override
public void onResourceReady(Z resource, Transition<? super Z> transition) {
HANDLER.obtainMessage(MESSAGE_CLEAR, this).sendToTarget();
}
private void clear() {
requestManager.clear(this);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册