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()); } };