Skip to content

Commit

Permalink
Fix race condition while loading messages
Browse files Browse the repository at this point in the history
Images didn't load correctly when the following scenario occurred:
* ApplicationHolder#request
* MessagesActivity.SelectApplicationAndUpdateMessages#execute
* MessagesActivity.SelectApplicationAndUpdateMessages#doInBackground
  - method returns List<MessageWithImage>, but images are "null" because
    apps aren't loaded yet.
* MessagesActivity#onUpdateApps
  - Now apps were loaded and cached.
* MessagesActivity.SelectApplicationAndUpdateMessages#onPostExecute
  - receives List<MessageWithImage> from #doInBackgroud with "null" images
-> Messages with "null" images are rendered in the ListView
  • Loading branch information
jmattheis committed Feb 17, 2019
1 parent 67daf8d commit fb9aa19
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ protected void onPostExecute(List<MessageWithImage> messageWithImages) {
}
}

private class SelectApplicationAndUpdateMessages
extends AsyncTask<Integer, Void, List<MessageWithImage>> {
private class SelectApplicationAndUpdateMessages extends AsyncTask<Integer, Void, Integer> {

private SelectApplicationAndUpdateMessages(boolean withLoadingSpinner) {
if (withLoadingSpinner) {
Expand All @@ -364,13 +363,15 @@ private SelectApplicationAndUpdateMessages(boolean withLoadingSpinner) {
}

@Override
protected List<MessageWithImage> doInBackground(Integer... appIds) {
return messages.getOrLoadMore(appIds[0]);
protected Integer doInBackground(Integer... appIds) {
Integer appId = first(appIds);
messages.loadMoreIfNotPresent(appId);
return appId;
}

@Override
protected void onPostExecute(List<MessageWithImage> messageWithImages) {
updateMessagesAndStopLoading(messageWithImages);
protected void onPostExecute(Integer appId) {
updateMessagesAndStopLoading(messages.get(appId));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ public List<MessageWithImage> loadMore(Integer appId) {
return get(appId);
}

public List<MessageWithImage> getOrLoadMore(Integer appId) {
public void loadMoreIfNotPresent(Integer appId) {
MessageState state = this.state.state(appId);
if (state.loaded) {
return get(appId);
if (!state.loaded) {
loadMore(appId);
}
return loadMore(appId);
}

public void clear() {
Expand Down

0 comments on commit fb9aa19

Please sign in to comment.