Skip to content

Cleanup index array #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class FirebaseIndexArray<T> extends CachingObservableSnapshotArray<T> imp
private static final String TAG = "FirebaseIndexArray";

private DatabaseReference mDataRef;
private Map<Query, ValueEventListener> mRefs = new HashMap<>();
private Map<DatabaseReference, ValueEventListener> mRefs = new HashMap<>();

private FirebaseArray<String> mKeySnapshots;
private List<DataSnapshot> mDataSnapshots = new ArrayList<>();
Expand Down Expand Up @@ -109,8 +109,8 @@ public void onCancelled(DatabaseError error) {
public void removeChangeEventListener(@NonNull ChangeEventListener listener) {
super.removeChangeEventListener(listener);
if (!isListening()) {
for (Query query : mRefs.keySet()) {
query.removeEventListener(mRefs.get(query));
for (DatabaseReference ref : mRefs.keySet()) {
ref.removeEventListener(mRefs.get(ref));
}

clearData();
Expand Down Expand Up @@ -150,7 +150,7 @@ private boolean isKeyAtIndex(String key, int index) {
}

protected void onKeyAdded(DataSnapshot data) {
Query ref = mDataRef.child(data.getKey());
DatabaseReference ref = mDataRef.child(data.getKey());

// Start listening
mRefs.put(ref, ref.addValueEventListener(new DataRefListener()));
Expand All @@ -162,20 +162,18 @@ protected void onKeyMoved(DataSnapshot data, int index, int oldIndex) {
if (isKeyAtIndex(key, oldIndex)) {
DataSnapshot snapshot = removeData(oldIndex);
mDataSnapshots.add(index, snapshot);
notifyChangeEventListeners(ChangeEventListener.EventType.MOVED,
snapshot,
index,
oldIndex);
notifyChangeEventListeners(EventType.MOVED, snapshot, index, oldIndex);
}
}

protected void onKeyRemoved(DataSnapshot data, int index) {
String key = data.getKey();
mDataRef.child(key).removeEventListener(mRefs.remove(mDataRef.getRef().child(key)));
ValueEventListener listener = mRefs.remove(mDataRef.getRef().child(key));
if (listener != null) mDataRef.child(key).removeEventListener(listener);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My app keeps crashing randomly with NPEs so I'm starting to think this could be getting called twice in really weird cases. Unfortunately, I've never been able to consistently reproduce it, but it can't hurt to add a null check.


if (isKeyAtIndex(key, index)) {
DataSnapshot snapshot = removeData(index);
notifyChangeEventListeners(ChangeEventListener.EventType.REMOVED, snapshot, index);
notifyChangeEventListeners(EventType.REMOVED, snapshot, index);
}
}

Expand Down Expand Up @@ -217,20 +215,20 @@ public void onDataChange(DataSnapshot snapshot) {
int index = getIndexForKey(key);

if (snapshot.getValue() != null) {
if (!isKeyAtIndex(key, index)) {
// We don't already know about this data, add it
mDataSnapshots.add(index, snapshot);
notifyChangeEventListeners(ChangeEventListener.EventType.ADDED, snapshot, index);
} else {
if (isKeyAtIndex(key, index)) {
// We already know about this data, just update it
updateData(index, snapshot);
notifyChangeEventListeners(ChangeEventListener.EventType.CHANGED, snapshot, index);
notifyChangeEventListeners(EventType.CHANGED, snapshot, index);
} else {
// We don't already know about this data, add it
mDataSnapshots.add(index, snapshot);
notifyChangeEventListeners(EventType.ADDED, snapshot, index);
}
} else {
if (isKeyAtIndex(key, index)) {
// This data has disappeared, remove it
removeData(index);
notifyChangeEventListeners(ChangeEventListener.EventType.REMOVED, snapshot, index);
notifyChangeEventListeners(EventType.REMOVED, snapshot, index);
} else {
// Data does not exist
Log.w(TAG, "Key not found at ref: " + snapshot.getRef());
Expand Down