1

I have a RecyclerView and each node contains images, that load in background:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.date.setText(this.nodes.get(position).getDate());
        holder.count.setText(Integer.toString(this.nodes.get(position).getImages().size()));
        if (this.nodes.get(position).getImages().size() > 0) {
            if (this.nodes.get(position).needLoad()) {
                holder.image.setVisibility(View.INVISIBLE);
                holder.pb.setVisibility(View.VISIBLE);
                new DbHelper.GetBuildingStatusNodeTask(getActivity(), this.nodes.get(position), activity).execute();
            } else {
                loadBitmap(this.nodes.get(position).getImages().get(0).getImagePath(), holder.image, holder.pb);
            }
        }
    }

DbHelper.GetBuildingStatusNodeTask() method load images from internet or from db (if there were cashed before) in background thread and then it calls callback method:

public void nodeLoadCallback(BuildingStatusNode callbackNode) {
    if (callbackNode != null) {
        for (int i = 0; i < nodes.size(); i++) {
            if (nodes.get(i).getId() == callbackNode.getId()) {
                nodes.set(i, callbackNode);
                mAdapter.notifyItemChanged(i);
                return;
            }
        }
    } else {
        // TODO error handler
    }
}

Also I have 1 more background method in onBindViewHolder - loadBitmap(), for smooth scrolling (from this tutorial).

So the question is next:

1) For some reasons loadBitmap() method is waiting while all DbHelper.GetBuildingStatusNodeTask() background tasks will be finished. For example, load for node with index 0 is done (I checked this in debugger) and loadBitmap() should update node, but it is waiting for another tasks to finish. Why?

2) Looks like when I am scrolling my recycleview I am making many similar threads for 1 specific node, what should I use to avoid this? ThreadQueue? Or simple Hashmap, that checks if there is a thread for specific index?

P.S. All my threads are AsyncTask class.

Panich Maxim
  • 1,105
  • 1
  • 15
  • 34

0 Answers0