It would be nice if the retrofit2.Callback interface could be broken down into two Single Abstract Method interfaces to make it possible to use lambda expressions and method references in Java 8 and Android with RetroLambda.
(I don't think that retrofit-adapters/java8 would work on Android.)
public interface SuccessCallback<T> {
void onResponse(Response<T> response);
}
public interface FailureCallback {
void onFailure(Throwable t);
}
public interface Call<T> extends Cloneable {
// current stuff
void enqueue(SuccessCallback<T> successCallback, FailureCallback failureCallback);
}
This is too drastic of a change to make this late in developer for far too little benefit (no benefit, in my opinion). Thankfully, we have a mechanism where you can create this yourself using a custom CallAdapter.Factory. See the ErrorHandlingCallAdapter example to get you started.
I would say it is a huge benefit to be able to use Java 8 style lambda expressions and method references for callbacks on Android (with RetroLambda).
You could support both this and the current Callback by overloading the enqueue method in Call, then it will be backwards compatible.
An HTTP call is a single unit of work and it delivers a single result (either a response or failure). As such, we actually value the coupling afforded by the dual-method callback.
I just want to be able to code like this:
call.enqueue(
response -> doStuffWith(response),
failure -> handle(failure)
);
@mikaelstaldal you can build your class to bridge this yourself.
class LambdaCallback {
public LambdaCallback(SuccessCallback sucess, FailureCallback failure) { ... }
}
call.enqueue(new LambdaCallback(
response -> doStuffWith(response),
failure -> handle(failure)));
@swankjesse can you explain your code with Retrofit callbacks? it doesn't work for me
Most helpful comment
I just want to be able to code like this: