Reputation: 10308
The recipes page for OkHttp states that when making an asynchronous request,
The callback is made after the response headers are ready. Reading the response body may still block.
Under what conditions would reading the response body block? Doesn't the possibility of blocking defeat the whole purpose of making an asynchronous call?
For those who have used OkHttp, how did you deal with the possibility of blocking?
AsyncTask
.Upvotes: 1
Views: 1159
Reputation: 76115
Under what conditions would reading the response body block?
When the body is larger than the remaining space after reading the headers in the 2k chunk size that's used.
Doesn't the possibility of blocking defeat the whole purpose of making an asynchronous call?
No. No one wants to write asynchronous code dealing with the processing of bytes.
When the response is received we hand you the source from which to read data from in a blocking way. Blocking IO code is much more easy to read, write, and reason about and most libraries in this space (like a Gson) are setup for blocking reads.
For those who have used OkHttp, how did you deal with the possibility of blocking?
You just do the blocking read of the body data and then notify application code through whatever mechanism you want when it's complete.
You mention AsyncTask which makes it seem like you're on Android. The callbacks don't happen on the main thread so doing blocking IO doesn't matter.
Upvotes: 2