Skip to content
This repository was archived by the owner on Jan 9, 2020. It is now read-only.

Commit

Permalink
Add a null check for onSuccess and onError Callbacks (#96)
Browse files Browse the repository at this point in the history
On a RosieUseCase, success and error callbacks are stored on WeakReferences
fields. These callbacks could be recycled before they are exicited
by the use cases to return the result of them, especially if the task are
really heavy, so it is necessary a null check before use them.
  • Loading branch information
Aracem authored and pedrovgs committed Jul 24, 2018
1 parent 463189c commit e8b44f7
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ public void setCallbackScheduler(CallbackScheduler callbackScheduler) {
* this method returns the response in the UI Thread.
*/
protected void notifySuccess(Object... values) {
Method[] methodsArray = onSuccessCallback.get().getClass().getMethods();
if (methodsArray.length > 0) {
Method methodToInvoke =
UseCaseFilter.filterValidMethodArgs(values, methodsArray, Success.class);
invokeMethodInTheCallbackScheduler(methodToInvoke, values);
} else {
throw new IllegalStateException(
"The OnSuccessCallback instance configured has no methods annotated with the "
+ "@Success annotation.");
OnSuccessCallback successCallback = onSuccessCallback.get();
if (successCallback != null) {
Method[] methodsArray = successCallback.getClass().getMethods();
if (methodsArray.length > 0) {
Method methodToInvoke =
UseCaseFilter.filterValidMethodArgs(values, methodsArray, Success.class);
invokeMethodInTheCallbackScheduler(methodToInvoke, values);
} else {
throw new IllegalStateException(
"The OnSuccessCallback instance configured has no methods annotated with the "
+ "@Success annotation.");
}
}
}

Expand All @@ -74,7 +77,10 @@ protected void notifySuccess(Object... values) {
*/

protected void notifyError(final Error error) throws ErrorNotHandledException {
onErrorCallback.get().onError(error);
OnErrorCallback errorCallback = onErrorCallback.get();
if (errorCallback != null) {
errorCallback.onError(error);
}
}

/**
Expand Down Expand Up @@ -104,12 +110,12 @@ void setOnErrorCallback(OnErrorCallback onErrorCallback) {
private void invokeMethodInTheCallbackScheduler(final Method methodToInvoke,
final Object[] values) {
if (onSuccessCallback != null) {
OnSuccessCallback callback = onSuccessCallback.get();
final OnSuccessCallback callback = onSuccessCallback.get();
if (callback != null) {
getCallbackScheduler().post(new Runnable() {
@Override public void run() {
try {
methodToInvoke.invoke(onSuccessCallback.get(), values);
methodToInvoke.invoke(callback, values);
} catch (Exception e) {
throw new RuntimeException("Internal error invoking the success object", e);
}
Expand Down

0 comments on commit e8b44f7

Please sign in to comment.