Skip to content

Commit f6ab0b9

Browse files
SUPERCILEXsamtstern
authored andcommitted
Fix bug where onDataChanged isn't called if no data is found at index key (#847)
1 parent 6fcf333 commit f6ab0b9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class FirebaseIndexArray<T> extends CachingObservableSnapshotArray<T> imp
4242
* contains keys that exist in the backing {@link FirebaseArray}, but their data hasn't been
4343
* downloaded yet in this array.
4444
*/
45-
private List<String> mKeysWithPendingData = new ArrayList<>();
45+
private List<String> mKeysWithPendingUpdate = new ArrayList<>();
4646
/**
4747
* Moves or deletions don't need to fetch new data so they can be performed instantly once the
4848
* backing {@link FirebaseArray} is done updating. This will be true if the backing {@link
@@ -165,7 +165,7 @@ protected void onKeyAdded(DataSnapshot data) {
165165
String key = data.getKey();
166166
DatabaseReference ref = mDataRef.child(key);
167167

168-
mKeysWithPendingData.add(key);
168+
mKeysWithPendingUpdate.add(key);
169169
// Start listening
170170
mRefs.put(ref, ref.addValueEventListener(new DataRefListener()));
171171
}
@@ -235,26 +235,29 @@ public void onDataChange(DataSnapshot snapshot) {
235235
// We already know about this data, just update it
236236
updateData(index, snapshot);
237237
notifyChangeEventListeners(EventType.CHANGED, snapshot, index);
238-
notifyListenersOnDataChanged();
239238
} else {
240239
// We don't already know about this data, add it
241240
mDataSnapshots.add(index, snapshot);
242241
notifyChangeEventListeners(EventType.ADDED, snapshot, index);
243-
244-
mKeysWithPendingData.remove(key);
245-
if (mKeysWithPendingData.isEmpty()) notifyListenersOnDataChanged();
246242
}
247243
} else {
248244
if (isKeyAtIndex(key, index)) {
249245
// This data has disappeared, remove it
250246
removeData(index);
251247
notifyChangeEventListeners(EventType.REMOVED, snapshot, index);
252-
notifyListenersOnDataChanged();
253248
} else {
254249
// Data does not exist
255250
Log.w(TAG, "Key not found at ref: " + snapshot.getRef());
256251
}
257252
}
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();
258261
}
259262

260263
@Override

0 commit comments

Comments
 (0)