Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async reading of ResponseBody #873

Closed
anaisbetts opened this issue May 28, 2014 · 4 comments
Closed

Async reading of ResponseBody #873

anaisbetts opened this issue May 28, 2014 · 4 comments
Labels
bug Bug in existing code
Milestone

Comments

@anaisbetts
Copy link

Hey guys, I'm coding up a ModernHttpClient adapter for OkHttp 2.0 and had a question.

So far, the new API is way cleaner to use, but one thing I'm not sure that I grok is the scope of ResponseBody's underlying stream. It seems that as soon as I leave onResponse, I either crash inside Okio, or receive an IOException back that the stream is closed. The straightforward code passes the byteStream result back to the caller (wrapped in a C# type), who then proceeds to read it on a separate thread - this dies.

It seems as if I can read the bytes out immediately after receiving the Response object synchronously, but this breaks the abstraction above it, which expects to be handed an object representing the headers + a Stream that the caller can read at its leisure.

I looked through the test suite but didn't see any uses of byteStream that weren't synchronous reads.

This also might be a Xamarin Thing™, definitely don't rule that out yet either :)

@swankjesse
Copy link
Collaborator

I've got to document that you cannot consume the response body after onResponse() returns. In this case your best bet is to do your own threading: Call.execute() instead of Call.enqueue().

Alternately, we let you consume the response body anywhere. That's pretty straightforward actually, though it means applications must call close() when they're done.

@anaisbetts
Copy link
Author

I've got to document that you cannot consume the response body after onResponse() returns.

That kind of kneecaps the async part of the API though, if you have to then block on a thread to read the response.

Alternately, we let you consume the response body anywhere. That's pretty straightforward actually, though it means applications must call close() when they're done.

That's not a big deal, I was assuming I'd have to do this anyways. The synchronous "Give me all the bytes" method can handle closing too, so only people who want the extra complexity have to think about the close

@swankjesse swankjesse added the bug label Jun 1, 2014
@swankjesse swankjesse added this to the 2.0 milestone Jun 1, 2014
@swankjesse
Copy link
Collaborator

#913

@anaisbetts
Copy link
Author

Nice, thanks @swankjesse!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

2 participants