coil icon indicating copy to clipboard operation
coil copied to clipboard

Avoid multiple parallel network requests for the same URL

Open Kazachenko1998 opened this issue 2 years ago • 6 comments

I am developing an application where there can be many identical pictures on one screen (SVG from the server). When I open the application for the first time, I see multiple parallel requests behind the same picture.

I think it's worth adding a pool of downloads, and if this URL is already being processed, do not make this request again, but wait for the first one to complete and use its result.

override fun onCreate(savedInstanceState: Bundle?) {
        CODE
        Coil.setImageLoader(
            ImageLoader
                .Builder(this)
                .components { add(SvgDecoder.Factory()) }
                .crossfade(true)
                .build()
        )
        CODE
}

AsyncImage(
    placeholder = rememberAsyncImagePainter(R.drawable.ic_plchldr),
    error = rememberAsyncImagePainter(R.drawable.ic_plchldr),
    model = URL,
    contentDescription = URL,
    contentScale = ContentScale.Crop,
    modifier = Modifier.fillMaxSize()
)

image

Kazachenko1998 avatar Sep 16 '22 14:09 Kazachenko1998

I agree this would be nice to have. Ideally, HttpUriFetcher should reference count the number of responses waiting for its network request. We probably can't merge requests that only share the same URL - we'll need request headers to match as well (at least by default).

Additionally - we can only do this for requests that are going to be written to the disk cache. If they're not being written to the disk cache then we can only read the response body once. There's a few other edge cases that means this would have to be opt-in at least for a while.

colinrtwhite avatar Sep 18 '22 03:09 colinrtwhite

Thinking about this more we should also provide a configurable "keep alive" duration for network requests. That way the user can configure how long to wait before cancelling a network request if there's no more consumer for the result.

This would be useful for RecyclerViews and other cases where a request can be restarted multiple times in quick succession.

colinrtwhite avatar Sep 28 '22 00:09 colinrtwhite

What is the status on this?

gargVader avatar May 10 '24 07:05 gargVader