From 4a1bb69ee0f17a20f99ec73d89fa62be0f7f6eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nelson=20R=2E=20P=C3=A9rez?= Date: Thu, 21 Nov 2019 16:28:51 -0500 Subject: [PATCH] Fixed an issue with the callback implementation - We're now passing the parsed response, whenever possible - The SparseArray was replaced by a HashMap --- .../graphenej/api/android/NetworkService.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index 6d6ac27..19172c9 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -120,7 +120,7 @@ public class NetworkService { private CompositeDisposable mCompositeDisposable; - private SparseArray mCallbackMap = new SparseArray(); + private HashMap mCallbackMap = new HashMap(); private Gson gson = new GsonBuilder() .registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer()) @@ -237,7 +237,7 @@ public class NetworkService { long id = this.sendMessage(apiCallable, requiredApi); if(callback != null){ if(id != -1){ - mCallbackMap.put((int) id, callback); + mCallbackMap.put(id, callback); }else{ callback.onFailure(new Exception("Message could not be sent"), null); } @@ -360,10 +360,11 @@ public class NetworkService { * @param response */ private void resetCallbacks(Throwable throwable, Response response){ - for(int i = 0; i < mCallbackMap.size(); i++){ - ApiCallback callback = mCallbackMap.get(i); - callback.onFailure(throwable, response); - mCallbackMap.remove(i); + for(ApiCallback callback : mCallbackMap.values()) { + if(callback != null) { + callback.onFailure(throwable, response); + mCallbackMap.remove(callback); + } } } @@ -482,13 +483,6 @@ public class NetworkService { private void handleJsonRpcResponse(JsonRpcResponse response, String text){ JsonRpcResponse parsedResponse = null; - // Executing callback, if present - if(mCallbackMap.indexOfKey((int) response.id) > 0){ - ApiCallback callback = (ApiCallback) mCallbackMap.get((int)response.id); - callback.onResponse(response, text); - mCallbackMap.remove((int)response.id); - } - Class requestClass = mRequestClassMap.get(response.id); if(requestClass != null){ // Removing the class entry in the map @@ -567,6 +561,14 @@ public class NetworkService { if(parsedResponse == null){ parsedResponse = response; } + + // Executing callback, if present with the parsed response + if(mCallbackMap.containsKey(response.id)){ + ApiCallback callback = mCallbackMap.get(response.id); + callback.onResponse(parsedResponse, text); + mCallbackMap.remove(response.id); + } + // Broadcasting the parsed response to all interested listeners RxBus.getBusInstance().send(parsedResponse); }