Skip to content

Commit 7e750ca

Browse files
sjuddglide-copybara-robot
authored andcommittedOct 31, 2019
Ignore callbacks from cancelled or otherwise invalid requests in SourceGenerator.
PiperOrigin-RevId: 277765705
1 parent 0acd87c commit 7e750ca

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed
 

‎library/src/main/java/com/bumptech/glide/load/engine/SourceGenerator.java

+39-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import android.util.Log;
44
import androidx.annotation.NonNull;
5+
import androidx.annotation.Nullable;
56
import com.bumptech.glide.load.DataSource;
67
import com.bumptech.glide.load.Encoder;
78
import com.bumptech.glide.load.Key;
89
import com.bumptech.glide.load.data.DataFetcher;
10+
import com.bumptech.glide.load.data.DataFetcher.DataCallback;
911
import com.bumptech.glide.load.model.ModelLoader;
1012
import com.bumptech.glide.load.model.ModelLoader.LoadData;
1113
import com.bumptech.glide.util.LogTime;
14+
import com.bumptech.glide.util.Synthetic;
1215
import java.util.Collections;
1316

1417
/**
@@ -19,10 +22,7 @@
1922
* <p>Depending on the disk cache strategy, source data may first be written to disk and then loaded
2023
* from the cache file rather than returned directly.
2124
*/
22-
class SourceGenerator
23-
implements DataFetcherGenerator,
24-
DataFetcher.DataCallback<Object>,
25-
DataFetcherGenerator.FetcherReadyCallback {
25+
class SourceGenerator implements DataFetcherGenerator, DataFetcherGenerator.FetcherReadyCallback {
2626
private static final String TAG = "SourceGenerator";
2727

2828
private final DecodeHelper<?> helper;
@@ -60,12 +60,40 @@ public boolean startNext() {
6060
&& (helper.getDiskCacheStrategy().isDataCacheable(loadData.fetcher.getDataSource())
6161
|| helper.hasLoadPath(loadData.fetcher.getDataClass()))) {
6262
started = true;
63-
loadData.fetcher.loadData(helper.getPriority(), this);
63+
startNextLoad(loadData);
6464
}
6565
}
6666
return started;
6767
}
6868

69+
private void startNextLoad(final LoadData<?> toStart) {
70+
loadData.fetcher.loadData(
71+
helper.getPriority(),
72+
new DataCallback<Object>() {
73+
@Override
74+
public void onDataReady(@Nullable Object data) {
75+
if (isCurrentRequest(toStart)) {
76+
onDataReadyInternal(toStart, data);
77+
}
78+
}
79+
80+
@Override
81+
public void onLoadFailed(@NonNull Exception e) {
82+
if (isCurrentRequest(toStart)) {
83+
onLoadFailedInternal(toStart, e);
84+
}
85+
}
86+
});
87+
}
88+
89+
// We want reference equality explicitly to make sure we ignore results from old requests.
90+
@SuppressWarnings({"PMD.CompareObjectsWithEquals", "WeakerAccess"})
91+
@Synthetic
92+
boolean isCurrentRequest(LoadData<?> requestLoadData) {
93+
LoadData<?> currentLoadData = loadData;
94+
return currentLoadData != null && currentLoadData == requestLoadData;
95+
}
96+
6997
private boolean hasNextModelLoader() {
7098
return loadDataListIndex < helper.getLoadData().size();
7199
}
@@ -107,8 +135,9 @@ public void cancel() {
107135
}
108136
}
109137

110-
@Override
111-
public void onDataReady(Object data) {
138+
@SuppressWarnings("WeakerAccess")
139+
@Synthetic
140+
void onDataReadyInternal(LoadData<?> loadData, Object data) {
112141
DiskCacheStrategy diskCacheStrategy = helper.getDiskCacheStrategy();
113142
if (data != null && diskCacheStrategy.isDataCacheable(loadData.fetcher.getDataSource())) {
114143
dataToCache = data;
@@ -125,8 +154,9 @@ public void onDataReady(Object data) {
125154
}
126155
}
127156

128-
@Override
129-
public void onLoadFailed(@NonNull Exception e) {
157+
@SuppressWarnings("WeakerAccess")
158+
@Synthetic
159+
void onLoadFailedInternal(LoadData<?> loadData, @NonNull Exception e) {
130160
cb.onDataFetcherFailed(originalKey, e, loadData.fetcher, loadData.fetcher.getDataSource());
131161
}
132162

0 commit comments

Comments
 (0)