Hello. I have a question regarding retrofit, maybe...
# squarelibraries
b
Hello. I have a question regarding retrofit, maybe okhttp. is there a possibility to block multiple requests to the same url when a request with this url is already running (e.g. on startup i call a config feed from multiple places and because this is done nearly the same time the request will call the server multiple times which wastes data traffic and battery). so what i want to do is fire the request only once and if the request is running and other places try to trigger this request too, the other requests should not be fired and will return the result of the first request if available. is this doable via an interceptor or something similiar? i am looking forward for your suggestions. thx
y
It's a bit dangerous by an interceptor, if you make some calls block, you will tie up OkHttp's Dispatcher threads.
I think it's cleanest done as a Call.Factory, outside the OkHttpClient. you can record the inflight calls. Reusing a response will be tricky, you might need to use the peek on the response. Definitely not simple.
s
1
y
Much better idea
b
@saket @yschimke Thanks for your suggestions. My initial idea also came from this library, but i do not need all of this features so i thought i could do the multiple request debouncing. but if there are no further advices or examples i can implement this also in my remoterepo
s
yea you can always roll out your own solution using a map of flows but having a battle tested solution is often a better idea, especially if you're low on time and this is a side project
1
j
Definitely agree this should be done above Retrofit, not within or below it. De-duplication of requests and the policy around which ones get de-duplicated and whether responses are cached and for how long is all application-specific stuff.
2
c
Yeah, I would think that if you have a
getCurrentConfig
method, you could possibly just put it in a synchronize block and call it a day? but i am still a noob when it comes to threading, so maybe that wouldn't work. but that would be my first attempt.