简体   繁体   中英

How to make Glide keep connection alive?

I am using the Android Glide library to load images remotely.

The image URL protocol is https so there is a performance implication due to TSL every time a new connection is established.

I'm using the okhttp integration:

def glide_version = "4.10.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"

Is it possible to make Glide keep the connection alive for better performance?

Try setting up an HTTP client with a custom ConnectionPool. Take a look at the connection pool's constructor:

public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit)

So you can try something like:

@GlideModule
class YourGlideModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry
                .replace(
                        GlideUrl::class.java,
                        InputStream::class.java,
                        OkHttpUrlLoader.Factory(
                                OkHttpClient.Builder()
                                        .connectionPool(ConnectionPool(5, 5, TimeUnit.MINUTES))
                                        .build()))
    }
}

Those are default values, but you can find the ones that suit you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM