0

In my app I was prefetching images so they load faster when user scrolls using prefetchToBitmapCache. Now when user applies a filter, these prefetches remain in pipeline and slow down the loading of new images.

How I am prefetching: When use scrolls down through images, I prefetch X number of images after LinearLayoutManager.findLastVisibleItemPosition()'s value. I need a way to cancel all these prefetches when user applies a filter. Is there a way to do this or a better way of prefetching?

Sourabh
  • 8,243
  • 10
  • 52
  • 98

1 Answers1

2

I am part of the Fresco team and may be able to help. Both imagePipeline.prefetchToDiskCache and imagePipeline.prefetchToBitmapCache return DataSource<Void> object. If you store the returned instance somewhere, you can just close it and that should cancel the prefetch.

// initiate prefetch
prefetchDataSource = imagePipeline.prefetchToBitmapCache(imageRequest, callerContext);

// cancel prefetch
prefetchDataSource.close();
plamenko
  • 1,068
  • 1
  • 8
  • 10
  • So I keep a reference to all prefetch requests and iterate over those and cancel then when I want to cancel prefetch? Will it throw any exception if the prefetch already finished? Also, can you consider doing something like Picasso for handling prefetch requests where they provide a tag and one can cancel all requests using that tag. PS: Also can you post the same [here](https://github.com/facebook/fresco/issues/794) – Sourabh Dec 03 '15 at 21:56
  • No exception will be thrown. If the prefetch is done, close will be a no-op. – plamenko Dec 05 '15 at 00:52
  • Seriously StackOverflow? I accidentally press enter and I have to wait for 5min in order to edit my comment? – plamenko Dec 05 '15 at 01:00
  • ...That is correct. No exception will be thrown, if the prefetch is done, `close` will be a no-op. We will look into how we can improve this API. Keep in mind however that what works for you may not work for others and so we need to carefully consider any API change. Anyways, until such API change is done (and there are no promises on if and when) cancelling each prefetch separately should work (and is the most flexible way anyway). If that answers your question, please consider accepting this answer so that others who may have the same question can see the solution. – plamenko Dec 05 '15 at 01:01