@@ -75,6 +75,7 @@ public final class FirestoreClient implements RemoteStore.RemoteStoreCallback {
75
75
private RemoteStore remoteStore ;
76
76
private SyncEngine syncEngine ;
77
77
private EventManager eventManager ;
78
+ private boolean isShutdown = false ;
78
79
79
80
// LRU-related
80
81
@ Nullable private LruGarbageCollector .Scheduler lruScheduler ;
@@ -125,15 +126,20 @@ public FirestoreClient(
125
126
}
126
127
127
128
public Task <Void > disableNetwork () {
129
+ this .verifyNotShutdown ();
128
130
return asyncQueue .enqueue (() -> remoteStore .disableNetwork ());
129
131
}
130
132
131
133
public Task <Void > enableNetwork () {
134
+ this .verifyNotShutdown ();
132
135
return asyncQueue .enqueue (() -> remoteStore .enableNetwork ());
133
136
}
134
137
135
138
/** Shuts down this client, cancels all writes / listeners, and releases all resources. */
136
139
public Task <Void > shutdown () {
140
+ if (this .isShutdown ) {
141
+ return Tasks .forResult (null );
142
+ }
137
143
credentialsProvider .removeChangeListener ();
138
144
return asyncQueue .enqueue (
139
145
() -> {
@@ -142,23 +148,27 @@ public Task<Void> shutdown() {
142
148
if (lruScheduler != null ) {
143
149
lruScheduler .stop ();
144
150
}
151
+ this .isShutdown = true ;
145
152
});
146
153
}
147
154
148
155
/** Starts listening to a query. */
149
156
public QueryListener listen (
150
157
Query query , ListenOptions options , EventListener <ViewSnapshot > listener ) {
158
+ this .verifyNotShutdown ();
151
159
QueryListener queryListener = new QueryListener (query , options , listener );
152
160
asyncQueue .enqueueAndForget (() -> eventManager .addQueryListener (queryListener ));
153
161
return queryListener ;
154
162
}
155
163
156
164
/** Stops listening to a query previously listened to. */
157
165
public void stopListening (QueryListener listener ) {
166
+ this .verifyNotShutdown ();
158
167
asyncQueue .enqueueAndForget (() -> eventManager .removeQueryListener (listener ));
159
168
}
160
169
161
170
public Task <Document > getDocumentFromLocalCache (DocumentKey docKey ) {
171
+ this .verifyNotShutdown ();
162
172
return asyncQueue
163
173
.enqueue (() -> localStore .readDocument (docKey ))
164
174
.continueWith (
@@ -180,6 +190,7 @@ public Task<Document> getDocumentFromLocalCache(DocumentKey docKey) {
180
190
}
181
191
182
192
public Task <ViewSnapshot > getDocumentsFromLocalCache (Query query ) {
193
+ this .verifyNotShutdown ();
183
194
return asyncQueue .enqueue (
184
195
() -> {
185
196
ImmutableSortedMap <DocumentKey , Document > docs = localStore .executeQuery (query );
@@ -196,6 +207,7 @@ public Task<ViewSnapshot> getDocumentsFromLocalCache(Query query) {
196
207
197
208
/** Writes mutations. The returned task will be notified when it's written to the backend. */
198
209
public Task <Void > write (final List <Mutation > mutations ) {
210
+ this .verifyNotShutdown ();
199
211
final TaskCompletionSource <Void > source = new TaskCompletionSource <>();
200
212
asyncQueue .enqueueAndForget (() -> syncEngine .writeMutations (mutations , source ));
201
213
return source .getTask ();
@@ -204,6 +216,7 @@ public Task<Void> write(final List<Mutation> mutations) {
204
216
/** Tries to execute the transaction in updateFunction up to retries times. */
205
217
public <TResult > Task <TResult > transaction (
206
218
Function <Transaction , Task <TResult >> updateFunction , int retries ) {
219
+ this .verifyNotShutdown ();
207
220
return AsyncQueue .callTask (
208
221
asyncQueue .getExecutor (),
209
222
() -> syncEngine .transaction (asyncQueue , updateFunction , retries ));
@@ -255,6 +268,12 @@ private void initialize(Context context, User user, boolean usePersistence, long
255
268
remoteStore .start ();
256
269
}
257
270
271
+ private void verifyNotShutdown () {
272
+ if (this .isShutdown ) {
273
+ throw new IllegalArgumentException ("The client has already been shutdown" );
274
+ }
275
+ }
276
+
258
277
@ Override
259
278
public void handleRemoteEvent (RemoteEvent remoteEvent ) {
260
279
syncEngine .handleRemoteEvent (remoteEvent );
0 commit comments