Skip to content

Commit 973df31

Browse files
author
Alexis Oyama
committed
Revert "fix(ANR): Moves saveRequestForLater from main thread. Adds synchroniz… (#162)"
This reverts commit 1a3e3b9.
1 parent bde96dd commit 973df31

File tree

4 files changed

+24
-57
lines changed

4 files changed

+24
-57
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/LeanplumEventDataManager.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import android.database.DatabaseUtils;
3030
import android.database.sqlite.SQLiteDatabase;
3131
import android.database.sqlite.SQLiteOpenHelper;
32-
import android.os.AsyncTask;
3332

3433
import com.leanplum.Leanplum;
3534
import com.leanplum.utils.SharedPreferencesUtil;
@@ -42,8 +41,6 @@
4241
import java.util.Locale;
4342
import java.util.Map;
4443
import java.util.UUID;
45-
import java.util.concurrent.Executor;
46-
import java.util.concurrent.Executors;
4744

4845
/**
4946
* LeanplumEventDataManager class to work with SQLite.
@@ -60,7 +57,6 @@ public class LeanplumEventDataManager {
6057
private static SQLiteDatabase database;
6158
private static LeanplumDataBaseManager databaseManager;
6259
private static ContentValues contentValues = new ContentValues();
63-
private static final Executor sqlLiteThreadExecutor = Executors.newSingleThreadExecutor();
6460

6561
static boolean willSendErrorLog = false;
6662

@@ -69,7 +65,7 @@ public class LeanplumEventDataManager {
6965
*
7066
* @param context Current context.
7167
*/
72-
public static synchronized void init(Context context) {
68+
public static void init(Context context) {
7369
if (database != null) {
7470
Log.e("Database is already initialized.");
7571
return;
@@ -185,17 +181,6 @@ private static void handleSQLiteError(String log, Throwable t) {
185181
}
186182
}
187183

188-
/**
189-
* Execute async task on single thread Executer.
190-
*
191-
* @param task Async task to execute.
192-
* @param params Params.
193-
*/
194-
static <T> void executeAsyncTask(AsyncTask<T, ?, ?> task,
195-
T... params) {
196-
task.executeOnExecutor(sqlLiteThreadExecutor, params);
197-
}
198-
199184
private static class LeanplumDataBaseManager extends SQLiteOpenHelper {
200185
LeanplumDataBaseManager(Context context) {
201186
super(context, DATABASE_NAME, null, DATABASE_VERSION);

AndroidSDKCore/src/main/java/com/leanplum/internal/Request.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -230,41 +230,30 @@ private Map<String, Object> createArgsDictionary() {
230230
return args;
231231
}
232232

233-
private void saveRequestForLater(final Map<String, Object> args) {
234-
final Request currentRequest = this;
235-
LeanplumEventDataManager.executeAsyncTask(new AsyncTask<Void, Void, Void>() {
236-
@Override
237-
protected Void doInBackground(Void... params) {
238-
try {
239-
synchronized (Request.class) {
240-
Context context = Leanplum.getContext();
241-
SharedPreferences preferences = context.getSharedPreferences(
242-
LEANPLUM, Context.MODE_PRIVATE);
243-
SharedPreferences.Editor editor = preferences.edit();
244-
long count = LeanplumEventDataManager.getEventsCount();
245-
String uuid = preferences.getString(Constants.Defaults.UUID_KEY, null);
246-
if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0) {
247-
uuid = UUID.randomUUID().toString();
248-
editor.putString(Constants.Defaults.UUID_KEY, uuid);
249-
SharedPreferencesUtil.commitChanges(editor);
250-
}
251-
args.put(UUID_KEY, uuid);
252-
LeanplumEventDataManager.insertEvent(JsonConverter.toJson(args));
253-
254-
dataBaseIndex = count;
255-
// Checks if here response and/or error callback for this request. We need to add callbacks to
256-
// eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
257-
// will handle error callback for this event.
258-
if (response != null || error != null && !Util.isConnected()) {
259-
eventCallbackManager.addCallbacks(currentRequest, response, error);
260-
}
261-
}
262-
} catch (Throwable t) {
263-
Util.handleException(t);
264-
}
265-
return null;
233+
private void saveRequestForLater(Map<String, Object> args) {
234+
synchronized (Request.class) {
235+
Context context = Leanplum.getContext();
236+
SharedPreferences preferences = context.getSharedPreferences(
237+
LEANPLUM, Context.MODE_PRIVATE);
238+
SharedPreferences.Editor editor = preferences.edit();
239+
long count = LeanplumEventDataManager.getEventsCount();
240+
String uuid = preferences.getString(Constants.Defaults.UUID_KEY, null);
241+
if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0) {
242+
uuid = UUID.randomUUID().toString();
243+
editor.putString(Constants.Defaults.UUID_KEY, uuid);
244+
SharedPreferencesUtil.commitChanges(editor);
266245
}
267-
});
246+
args.put(UUID_KEY, uuid);
247+
LeanplumEventDataManager.insertEvent(JsonConverter.toJson(args));
248+
249+
dataBaseIndex = count;
250+
// Checks if here response and/or error callback for this request. We need to add callbacks to
251+
// eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
252+
// will handle error callback for this event.
253+
if (response != null || error != null && !Util.isConnected()) {
254+
eventCallbackManager.addCallbacks(this, response, error);
255+
}
256+
}
268257
}
269258

270259
public void send() {

AndroidSDKTests/src/test/java/com/leanplum/__setup/AbstractTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ public void before() throws Exception {
130130
// Mock with our executor which will run on main thread.
131131
ReflectionHelpers.setStaticField(Util.class, "asyncExecutor", new SynchronousExecutor());
132132
ReflectionHelpers.setStaticField(Util.class, "singleThreadExecutor", new SynchronousExecutor());
133-
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "sqlLiteThreadExecutor",
134-
new SynchronousExecutor());
135-
136133
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "databaseManager", null);
137134
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "database", null);
138135
// Get and set application context.

AndroidSDKTests/src/test/java/com/leanplum/internal/RequestTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.leanplum.Leanplum;
2626
import com.leanplum.__setup.LeanplumTestApp;
27-
import com.leanplum._whitebox.utilities.SynchronousExecutor;
2827

2928
import junit.framework.TestCase;
3029

@@ -35,7 +34,6 @@
3534
import org.robolectric.RobolectricTestRunner;
3635
import org.robolectric.RuntimeEnvironment;
3736
import org.robolectric.annotation.Config;
38-
import org.robolectric.util.ReflectionHelpers;
3937

4038
import java.lang.reflect.InvocationTargetException;
4139
import java.lang.reflect.Method;
@@ -67,8 +65,6 @@ public void setUp() {
6765
Application context = RuntimeEnvironment.application;
6866
assertNotNull(context);
6967
Leanplum.setApplicationContext(context);
70-
ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "sqlLiteThreadExecutor", new SynchronousExecutor());
71-
LeanplumEventDataManagerTest.setDatabaseToNull();
7268
}
7369

7470
/**

0 commit comments

Comments
 (0)