Skip to content

Commit

Permalink
Allow removing size callbacks from Targets.
Browse files Browse the repository at this point in the history
There's a race where a Target can be cleared before a View is laid out. If that happens and the view is laid out later on, we will crash. Removing the callback from the ViewTarget when the Target is cleared resolves that issue.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=155384032
  • Loading branch information
sjudd authored and Sam Judd committed May 15, 2017
1 parent 360757e commit 75a28f1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/src/main/java/com/bumptech/glide/ListPreloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,10 @@ public void onResourceReady(Object resource, Transition<? super Object> transiti
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(photoWidth, photoHeight);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
// Do nothing because we don't retain references to SizeReadyCallbacks.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(width, height);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
// Do nothing because we do not retain references to SizeReadyCallbacks.
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ && canNotifyStatusChanged()) {
*/
void cancel() {
stateVerifier.throwIfRecycled();
target.removeCallback(this);
status = Status.CANCELLED;
if (loadStatus != null) {
loadStatus.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public final void getSize(SizeReadyCallback cb) {
}
cb.onSizeReady(width, height);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
// Do nothing, we never retain a reference to the callback.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public interface Target<R> extends LifecycleListener {
*/
void getSize(SizeReadyCallback cb);

/**
* Removes the given callback from the pending set if it's still retained.
*
* @param cb The callback to remove.
*/
void removeCallback(SizeReadyCallback cb);

/**
* Sets the current request for this target to retain, should not be called outside of Glide.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public void getSize(SizeReadyCallback cb) {
sizeDeterminer.getSize(cb);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
sizeDeterminer.removeCallback(cb);
}

@Override
public void onLoadCleared(Drawable placeholder) {
super.onLoadCleared(placeholder);
Expand Down Expand Up @@ -214,6 +219,10 @@ void getSize(SizeReadyCallback cb) {
}
}

void removeCallback(SizeReadyCallback cb) {
cbs.remove(cb);
}

void clearCallbacksAndListener() {
// Keep a reference to the layout listener and remove it here
// rather than having the observer remove itself because the observer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,11 @@ public void onResourceReady(List resource, Transition<? super List> transition)
public void getSize(SizeReadyCallback cb) {
}

@Override
public void removeCallback(SizeReadyCallback cb) {
// Do nothing.
}

@Override
public void setRequest(Request request) {
}
Expand Down

0 comments on commit 75a28f1

Please sign in to comment.