Skip to content

Commit 40191c0

Browse files
author
Joe Hansche
committed
Fix #21: connect and disconnect on the background executor
TubeSock already uses a background thread for `open()`, but it keeps the `close()` call on the calling thread. So `open()` is safe on the main thread, but `close()` is not.
1 parent 142774e commit 40191c0

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,21 @@ public void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling su
8888

8989
@Override
9090
public void reconnect() {
91-
if (webSocketClient != null) {
92-
webSocketClient.close();
93-
}
94-
this.webSocketClient = webSocketClientFactory.createInstance(webSocketClientCallback, uri);
95-
this.webSocketClient.open();
91+
disconnectAsync().continueWith(new Continuation<Void, Void>() {
92+
@Override
93+
public Void then(Task<Void> task) throws Exception {
94+
webSocketClient = webSocketClientFactory.createInstance(webSocketClientCallback, uri);
95+
webSocketClient.open();
96+
return null;
97+
}
98+
});
9699
userInitiatedDisconnect = false;
97100
}
98101

99102
@Override
100103
public void disconnect() {
101104
if (webSocketClient != null) {
102-
webSocketClient.close();
103-
webSocketClient = null;
105+
disconnectAsync();
104106
userInitiatedDisconnect = true;
105107
}
106108
}
@@ -131,6 +133,20 @@ public Void call() throws Exception {
131133
}, taskExecutor);
132134
}
133135

136+
private Task<Void> disconnectAsync() {
137+
return Task.call(new Callable<Void>() {
138+
@Override
139+
public Void call() throws Exception {
140+
if (webSocketClient != null) {
141+
webSocketClient.close();
142+
webSocketClient = null;
143+
}
144+
145+
return null;
146+
}
147+
}, taskExecutor);
148+
}
149+
134150
private void parseMessage(String message) throws LiveQueryException {
135151
try {
136152
JSONObject jsonObject = new JSONObject(message);
@@ -227,7 +243,7 @@ private void sendSubscription(final Subscription<T> subscription) {
227243
public Void then(Task<String> task) throws Exception {
228244
String sessionToken = task.getResult();
229245
SubscribeClientOperation<T> op = new SubscribeClientOperation<>(subscription.getRequestId(), subscription.getQueryState(), sessionToken);
230-
246+
231247
// dispatch errors
232248
sendOperationAsync(op).continueWith(new Continuation<Void, Void>() {
233249
public Void then(Task<Void> task) {

0 commit comments

Comments
 (0)