@@ -42,7 +42,7 @@ public class FirebaseIndexArray<T> extends CachingObservableSnapshotArray<T> imp
42
42
* contains keys that exist in the backing {@link FirebaseArray}, but their data hasn't been
43
43
* downloaded yet in this array.
44
44
*/
45
- private List <String > mKeysWithPendingData = new ArrayList <>();
45
+ private List <String > mKeysWithPendingUpdate = new ArrayList <>();
46
46
/**
47
47
* Moves or deletions don't need to fetch new data so they can be performed instantly once the
48
48
* backing {@link FirebaseArray} is done updating. This will be true if the backing {@link
@@ -165,7 +165,7 @@ protected void onKeyAdded(DataSnapshot data) {
165
165
String key = data .getKey ();
166
166
DatabaseReference ref = mDataRef .child (key );
167
167
168
- mKeysWithPendingData .add (key );
168
+ mKeysWithPendingUpdate .add (key );
169
169
// Start listening
170
170
mRefs .put (ref , ref .addValueEventListener (new DataRefListener ()));
171
171
}
@@ -235,26 +235,29 @@ public void onDataChange(DataSnapshot snapshot) {
235
235
// We already know about this data, just update it
236
236
updateData (index , snapshot );
237
237
notifyChangeEventListeners (EventType .CHANGED , snapshot , index );
238
- notifyListenersOnDataChanged ();
239
238
} else {
240
239
// We don't already know about this data, add it
241
240
mDataSnapshots .add (index , snapshot );
242
241
notifyChangeEventListeners (EventType .ADDED , snapshot , index );
243
-
244
- mKeysWithPendingData .remove (key );
245
- if (mKeysWithPendingData .isEmpty ()) notifyListenersOnDataChanged ();
246
242
}
247
243
} else {
248
244
if (isKeyAtIndex (key , index )) {
249
245
// This data has disappeared, remove it
250
246
removeData (index );
251
247
notifyChangeEventListeners (EventType .REMOVED , snapshot , index );
252
- notifyListenersOnDataChanged ();
253
248
} else {
254
249
// Data does not exist
255
250
Log .w (TAG , "Key not found at ref: " + snapshot .getRef ());
256
251
}
257
252
}
253
+
254
+ // In theory, we would only want to pop the queue if this listener was just added
255
+ // i.e. `snapshot.value != null && isKeyAtIndex(...)`. However, if the developer makes a
256
+ // mistake and `snapshot.value == null`, we will never pop the queue and
257
+ // `notifyListenersOnDataChanged()` will never be called. Thus, we pop the queue anytime
258
+ // an update is received.
259
+ mKeysWithPendingUpdate .remove (key );
260
+ if (mKeysWithPendingUpdate .isEmpty ()) notifyListenersOnDataChanged ();
258
261
}
259
262
260
263
@ Override
0 commit comments