From 4d5096d9805cfb1df605fce500af80503f5e8e34 Mon Sep 17 00:00:00 2001 From: Grantland Chew Date: Wed, 19 Aug 2015 18:45:30 -0700 Subject: [PATCH] Don't share locks across UI thread and long running processes `setConnected` uses the same lock we use for synchronizing disk I/O, so there's a possibility that we can block the UI thread on disk I/O, so we're going to bump the lock usage to a different thread. --- .../java/com/parse/ParseCommandCache.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Parse/src/main/java/com/parse/ParseCommandCache.java b/Parse/src/main/java/com/parse/ParseCommandCache.java index 38644aa00..2ca798839 100644 --- a/Parse/src/main/java/com/parse/ParseCommandCache.java +++ b/Parse/src/main/java/com/parse/ParseCommandCache.java @@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.HashMap; +import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; @@ -93,13 +94,30 @@ public static int getPendingCount() { ConnectivityNotifier.ConnectivityListener listener = new ConnectivityNotifier.ConnectivityListener() { @Override public void networkConnectivityStatusChanged(Context context, Intent intent) { - boolean connectionLost = + final boolean connectionLost = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); - if (connectionLost) { - setConnected(false); - } else { - setConnected(ConnectivityNotifier.isConnected(context)); - } + final boolean isConnected = ConnectivityNotifier.isConnected(context); + + /* + Hack to avoid blocking the UI thread with disk I/O + + setConnected uses the same lock we use for synchronizing disk I/O, so there's a possibility + that we can block the UI thread on disk I/O, so we're going to bump the lock usage to a + different thread. + + TODO(grantland): Convert to TaskQueue, similar to ParsePinningEventuallyQueue + */ + Task.call(new Callable() { + @Override + public Void call() throws Exception { + if (connectionLost) { + setConnected(false); + } else { + setConnected(isConnected); + } + return null; + } + }, ParseExecutors.io()); } };