2020 */
2121package com .leanplum .internal ;
2222
23+ import android .os .AsyncTask ;
2324import android .support .annotation .NonNull ;
2425
2526import org .json .JSONObject ;
@@ -64,7 +65,7 @@ void addCallbacks(Request request, Request.ResponseCallback responseCallback,
6465 * @param error Exception.
6566 * @param countOfEvents Count of events that we got from database.
6667 */
67- void invokeAllCallbacksWithError (@ NonNull Exception error , int countOfEvents ) {
68+ void invokeAllCallbacksWithError (@ NonNull final Exception error , int countOfEvents ) {
6869 if (eventCallbacks .size () == 0 ) {
6970 return ;
7071 }
@@ -73,12 +74,20 @@ void invokeAllCallbacksWithError(@NonNull Exception error, int countOfEvents) {
7374 eventCallbacks .entrySet ().iterator ();
7475 // Loop over all callbacks.
7576 for (; iterator .hasNext (); ) {
76- Map .Entry <Request , LeanplumEventCallbacks > entry = iterator .next ();
77+ final Map .Entry <Request , LeanplumEventCallbacks > entry = iterator .next ();
7778 if (entry .getKey ().getDataBaseIndex () >= countOfEvents ) {
7879 entry .getKey ().setDataBaseIndex (entry .getKey ().getDataBaseIndex () - countOfEvents );
7980 } else {
8081 if (entry .getValue () != null && entry .getValue ().errorCallback != null ) {
81- entry .getValue ().errorCallback .error (error );
82+ // Start callback asynchronously, to avoid creation of new Request object from the same
83+ // thread.
84+ Util .executeAsyncTask (false , new AsyncTask <Void , Void , Void >() {
85+ @ Override
86+ protected Void doInBackground (Void ... params ) {
87+ entry .getValue ().errorCallback .error (error );
88+ return null ;
89+ }
90+ });
8291 }
8392 iterator .remove ();
8493 }
@@ -92,7 +101,7 @@ void invokeAllCallbacksWithError(@NonNull Exception error, int countOfEvents) {
92101 * @param responseBody JSONObject withs server response.
93102 * @param countOfEvents Count of events that we got from database.
94103 */
95- void invokeAllCallbacksForResponse (@ NonNull JSONObject responseBody , int countOfEvents ) {
104+ void invokeAllCallbacksForResponse (@ NonNull final JSONObject responseBody , int countOfEvents ) {
96105 if (eventCallbacks .size () == 0 ) {
97106 return ;
98107 }
@@ -101,12 +110,21 @@ void invokeAllCallbacksForResponse(@NonNull JSONObject responseBody, int countOf
101110 eventCallbacks .entrySet ().iterator ();
102111 // Loop over all callbacks.
103112 for (; iterator .hasNext (); ) {
104- Map .Entry <Request , LeanplumEventCallbacks > entry = iterator .next ();
113+ final Map .Entry <Request , LeanplumEventCallbacks > entry = iterator .next ();
105114 if (entry .getKey ().getDataBaseIndex () >= countOfEvents ) {
106115 entry .getKey ().setDataBaseIndex (entry .getKey ().getDataBaseIndex () - countOfEvents );
107116 } else {
108117 if (entry .getValue () != null && entry .getValue ().responseCallback != null ) {
109- entry .getValue ().responseCallback .response (responseBody );
118+ // Start callback asynchronously, to avoid creation of new Request object from the same
119+ // thread.
120+ Util .executeAsyncTask (false , new AsyncTask <Void , Void , Void >() {
121+ @ Override
122+ protected Void doInBackground (Void ... params ) {
123+ entry .getValue ().responseCallback .response (Request .getResponseAt (responseBody ,
124+ (int ) entry .getKey ().getDataBaseIndex ()));
125+ return null ;
126+ }
127+ });
110128 }
111129 iterator .remove ();
112130 }
0 commit comments