Skip to content

Commit 143a849

Browse files
authored
Merge pull request #27 from cloudinary/fix/sample-app-race-condition
Fix bug in sample app (trying to access a deleted resource).
2 parents 5bd5e65 + 3c6ef25 commit 143a849

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

sample/src/main/java/com/cloudinary/android/sample/app/CloudinaryService.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ private void cancelNotification(String requestId) {
135135
}
136136

137137
private boolean sendBroadcast(Resource updatedResource) {
138-
return LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_RESOURCE_MODIFIED).putExtra("resource", updatedResource));
138+
// This is called from background threads and the main thread may touch the resource and delete it
139+
// in the meantime (from the activity) - verify it's still around before sending the broadcast
140+
if (updatedResource != null) {
141+
return LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_RESOURCE_MODIFIED).putExtra("resource", updatedResource));
142+
}
143+
144+
return false;
139145
}
140146

141147
@Override
@@ -218,15 +224,11 @@ public void run() {
218224
resource = ResourceRepo.getInstance().resourceFailed(requestId, error.getCode(), error.getDescription());
219225
}
220226

221-
// resource may be deleted if it was cancelled in the activity
222-
if (resource != null) {
223-
sendBroadcast(resource);
224-
}
225-
227+
sendBroadcast(resource);
226228
}
227229
});
228-
cancelNotification(requestId);
229230

231+
cancelNotification(requestId);
230232

231233
int id = idsProvider.incrementAndGet();
232234
requestIdsToNotificationIds.put(requestId, id);

0 commit comments

Comments
 (0)