Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of a singleton instance OkHttpClient

Tags:

android

okhttp

I'm using OkHttp at the first time. I see the tutorial said that If wanna use Response Caching, I must call new OkHttpClient() exactly once (a singleton instance). OkhttpClient is thread-safe by synchronized methods. But in my application, there are many threads connects to the network to get remote data simultaneously, some threads must wait for a thread have done getting data to execute its operation.
So Is it's performance not better than normal?
If yes, If I don't enable Reponse Caching, should I call new OkHttpClient() many times for better performance?
Thanks

like image 660
Huy Duong Tu Avatar asked Jul 17 '26 07:07

Huy Duong Tu


1 Answers

For the best performance, share a single OkHttpClient instance. This allows your cache to be shared, and in the future when we implement fancy joining & cancelling it’ll allow outgoing requests to be merged.

like image 56
Jesse Wilson Avatar answered Jul 18 '26 21:07

Jesse Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!