-
Notifications
You must be signed in to change notification settings - Fork 753
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
Rx and how to know if Request has completed #247
Comments
so i now tried to use a custom implementation of Looking at the implementation of ResponseDeliveryRunnable it looked promising to actually have a custom implementation of ResponseDeliveryRunnable. However, then we'd need access to package private methods like finish and deliverResponse. As suggested here #70 next attempt is to extend ExecutorDelivery and enrich it with a custom runnable that would run here. This would work for so i change my custom request implementation to @Override
public void cancel() {
super.cancel(error);
this.onComplete();
}
@Override
public void deliverError(VolleyError error) {
super.deliverError(error);
this.onError(error);
this.onComplete();
}
@Override
protected void deliverResponse(R response) {
super.deliverResponse(response);
this.onNext(response);
} and then implement the custom ResponseDelivery like this public class CustomResponseDelivery extends ExecutorDelivery {
public CustomResponseDelivery(Handler handler) {
super(handler);
}
public CustomResponseDelivery(Executor executor) {
super(executor);
}
@Override
public void postResponse(com.android.volley.Request<?> request, Response<?> response, Runnable runnable) {
super.postResponse(request, response, new PostRunnable(runnable, request, response));
}
private static class PostRunnable implements Runnable {
private final Runnable otherRunnable;
private final Request<?> request;
private final Response<?> response;
PostRunnable(Runnable otherRunnable, Request<?> request, Response<?> response) {
this.otherRunnable = otherRunnable;
this.request = request;
this.response = response;
}
@Override
public void run() {
if (request instanceof MyRequest && (request.isCanceled() || !response.intermediate)) {
((MyRequest) request).onComplete();
}
if (otherRunnable != null) {
otherRunnable.run();
}
}
}
} This is all pseudocode so i still have to fine tune the idea and test it. It would be very nice if we could have control over methods like |
Yeah, I think this is a bit tricky with current APIs, since you want to see Response#intermediate, but this detail is abstracted away unless you are implementing a custom ResponseDelivery as you've done. Another approach you might explore would be to look at Request#getCacheEntry. When an intermediate response is delivered from the cache, getCacheEntry will return the CacheEntry. For final responses (either from the cache, or the network), it'll return null. So, when the first response comes in, either getCacheEntry is null, in which case you should get no further callbacks, or it is not, in which case you set a flag to expect one more callback. This is a bit hacky, though, and I'd hesitate to rely on it, even if it is simpler. Leaving this open as a longer-term feature request to more easily know whether a response is intermediate or not. Unfortunately I'm not sure if there will be an easy way to do this without breaking existing APIs, since there's currently no object with which we can associate the information that is passed where needed. |
It could also be an expired cache entry that gets added to the request (for not-modified responses). In that case, the response would be the final, network response but the request still has the entry. But if you just want to do something when the request is finished, I think there is an easier way, I haven't used it so I'm not quite sure: If you build Volley from the current version, you can add a RequestEventListener and listen for RequestEvent.REQUEST_FINISHED which the RequestQueue sends in finish. |
an ability to set a
|
RequestEvent.REQUEST_FINISHED is exactly what i would need. However even with current version same is possible via |
When overriding a Request we have the power to override the following methods
I want to write a custom request that enables reactive programming. I want my request to emit values
onNext
, emit errorsonError
and emit completenesonComplete
. My first attempt is the following code.Since
deliverResponse
is called for both cached and network responses and there is no way to distinguish between them (or is there?) we can not reliably callonComplete
after delivery.Maybe i am missing something.
The text was updated successfully, but these errors were encountered: