17
17
import static com .google .firebase .firestore .util .Assert .hardAssert ;
18
18
19
19
import android .support .annotation .Nullable ;
20
+ import android .util .SparseArray ;
20
21
import com .google .firebase .database .collection .ImmutableSortedSet ;
21
22
import com .google .firebase .firestore .core .OnlineState ;
22
23
import com .google .firebase .firestore .core .Transaction ;
@@ -118,7 +119,7 @@ public interface RemoteStoreCallback {
118
119
* removed with unlistens are removed eagerly without waiting for confirmation from the listen
119
120
* stream.
120
121
*/
121
- private final Map < Integer , QueryData > listenTargets ;
122
+ private final SparseArray < QueryData > listenTargets ;
122
123
123
124
private final OnlineStateTracker onlineStateTracker ;
124
125
@@ -152,7 +153,7 @@ public RemoteStore(
152
153
this .localStore = localStore ;
153
154
this .datastore = datastore ;
154
155
155
- listenTargets = new HashMap < >();
156
+ listenTargets = new SparseArray < QueryData >();
156
157
writePipeline = new ArrayDeque <>();
157
158
158
159
onlineStateTracker =
@@ -292,8 +293,7 @@ public void handleCredentialChange() {
292
293
/** Listens to the target identified by the given QueryData. */
293
294
public void listen (QueryData queryData ) {
294
295
Integer targetId = queryData .getTargetId ();
295
- hardAssert (
296
- !listenTargets .containsKey (targetId ),
296
+ hardAssert (listenTargets .get (targetId ) == null ,
297
297
"listen called with duplicate target ID: %d" ,
298
298
targetId );
299
299
@@ -318,7 +318,8 @@ private void sendWatchRequest(QueryData queryData) {
318
318
* be torn down after one minute of inactivity.
319
319
*/
320
320
public void stopListening (int targetId ) {
321
- QueryData queryData = listenTargets .remove (targetId );
321
+ QueryData queryData = listenTargets .get (targetId );
322
+ listenTargets .remove (targetId );
322
323
hardAssert (
323
324
queryData != null , "stopListening called on target no currently watched: %d" , targetId );
324
325
@@ -327,7 +328,7 @@ public void stopListening(int targetId) {
327
328
sendUnwatchRequest (targetId );
328
329
}
329
330
330
- if (listenTargets .isEmpty () ) {
331
+ if (listenTargets .size ()== 0 ) {
331
332
if (watchStream .isOpen ()) {
332
333
watchStream .markIdle ();
333
334
} else if (this .canUseNetwork ()) {
@@ -357,7 +358,7 @@ private boolean shouldStartWriteStream() {
357
358
* active watch targets.
358
359
*/
359
360
private boolean shouldStartWatchStream () {
360
- return canUseNetwork () && !watchStream .isStarted () && ! listenTargets .isEmpty () ;
361
+ return canUseNetwork () && !watchStream .isStarted () && listenTargets .size () != 0 ;
361
362
}
362
363
363
364
private void cleanUpWatchStreamState () {
@@ -378,9 +379,10 @@ private void startWatchStream() {
378
379
}
379
380
380
381
private void handleWatchStreamOpen () {
382
+ final int nsize = listenTargets .size ();
381
383
// Restore any existing watches.
382
- for ( QueryData queryData : listenTargets . values () ) {
383
- sendWatchRequest (queryData );
384
+ for ( int i = 0 ; i < nsize ; i ++ ) {
385
+ sendWatchRequest (listenTargets . get ( i ) );
384
386
}
385
387
}
386
388
@@ -515,7 +517,7 @@ private void processTargetError(WatchTargetChange targetChange) {
515
517
hardAssert (targetChange .getCause () != null , "Processing target error without a cause" );
516
518
for (Integer targetId : targetChange .getTargetIds ()) {
517
519
// Ignore targets that have been removed already.
518
- if (listenTargets .containsKey (targetId )) {
520
+ if (listenTargets .get (targetId ) != null ) {
519
521
listenTargets .remove (targetId );
520
522
watchChangeAggregator .removeTarget (targetId );
521
523
remoteStoreCallback .handleRejectedListen (targetId , targetChange .getCause ());
0 commit comments