android - Deduplicating Fresco image load request when using ImageRequest and ImageView - Stack Overflow

admin2025-04-18  3

I am using Fresco 2.5 and I am downloading image for URL into android ImageView. Unfortunately, I can't use SimpleDrawee.

How do I defer another request for the same URL if the previous request has not finished?

I could create concurrent map where I put url and ImageRequest in map. And if imageRequest for url exist do nothing, and on completion or failure remove the entry from the map.

What other way folks that use Fresco are deplicating the request. Glide has such a nice RequestManager class that handles all of that. I am surprised that Fresco doesn't have such a thing.

Perhaps I have missed something in the documentation that does this already.Would appreciate any suggestions.

I have code like this that doesn't do any map operation yet.

    private fun loadImageUsingFresco(
        url: String,
        imageView: ImageView,
        callback: IImageLoadedCallback?,
    ) {
        val startTime = SystemClock.elapsedRealtime()

        val imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build()
        val dataSource =
            Fresco.getImagePipeline().fetchDecodedImage(imageRequest, imageViewContext)

        dataSource.subscribe(
            object : BaseBitmapDataSubscriber() {
                override fun onNewResultImpl(bitmap: Bitmap?) {
                    if (dataSource.isFinished && bitmap != null) {
                        runOnUiThread {
                            imageView.setImageBitmap(bitmap)
                            
                            callback?.onSuccess()
                        }
                    }
                }

                override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>) {
                   callback?.onFailure(url)
                }
            },
            CallerThreadExecutor.getInstance(),
        )
    }
转载请注明原文地址:http://anycun.com/QandA/1744949848a89909.html