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

Commit e8b44f7

Browse files
Aracempedrovgs
authored andcommitted
Add a null check for onSuccess and onError Callbacks (#96)
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.
1 parent 463189c commit e8b44f7

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

rosie/src/main/java/com/karumi/rosie/domain/usecase/RosieUseCase.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ public void setCallbackScheduler(CallbackScheduler callbackScheduler) {
5252
* this method returns the response in the UI Thread.
5353
*/
5454
protected void notifySuccess(Object... values) {
55-
Method[] methodsArray = onSuccessCallback.get().getClass().getMethods();
56-
if (methodsArray.length > 0) {
57-
Method methodToInvoke =
58-
UseCaseFilter.filterValidMethodArgs(values, methodsArray, Success.class);
59-
invokeMethodInTheCallbackScheduler(methodToInvoke, values);
60-
} else {
61-
throw new IllegalStateException(
62-
"The OnSuccessCallback instance configured has no methods annotated with the "
63-
+ "@Success annotation.");
55+
OnSuccessCallback successCallback = onSuccessCallback.get();
56+
if (successCallback != null) {
57+
Method[] methodsArray = successCallback.getClass().getMethods();
58+
if (methodsArray.length > 0) {
59+
Method methodToInvoke =
60+
UseCaseFilter.filterValidMethodArgs(values, methodsArray, Success.class);
61+
invokeMethodInTheCallbackScheduler(methodToInvoke, values);
62+
} else {
63+
throw new IllegalStateException(
64+
"The OnSuccessCallback instance configured has no methods annotated with the "
65+
+ "@Success annotation.");
66+
}
6467
}
6568
}
6669

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

7679
protected void notifyError(final Error error) throws ErrorNotHandledException {
77-
onErrorCallback.get().onError(error);
80+
OnErrorCallback errorCallback = onErrorCallback.get();
81+
if (errorCallback != null) {
82+
errorCallback.onError(error);
83+
}
7884
}
7985

8086
/**
@@ -104,12 +110,12 @@ void setOnErrorCallback(OnErrorCallback onErrorCallback) {
104110
private void invokeMethodInTheCallbackScheduler(final Method methodToInvoke,
105111
final Object[] values) {
106112
if (onSuccessCallback != null) {
107-
OnSuccessCallback callback = onSuccessCallback.get();
113+
final OnSuccessCallback callback = onSuccessCallback.get();
108114
if (callback != null) {
109115
getCallbackScheduler().post(new Runnable() {
110116
@Override public void run() {
111117
try {
112-
methodToInvoke.invoke(onSuccessCallback.get(), values);
118+
methodToInvoke.invoke(callback, values);
113119
} catch (Exception e) {
114120
throw new RuntimeException("Internal error invoking the success object", e);
115121
}

0 commit comments

Comments
 (0)