Skip to content

Add call to onDataChanged when adding first listener #736

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 3 commits into from
Jun 8, 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 @@ -23,6 +23,8 @@ public abstract class ObservableSnapshotArray<E> extends ImmutableList<DataSnaps
protected final List<ChangeEventListener> mListeners = new CopyOnWriteArrayList<>();
protected final SnapshotParser<E> mParser;

private boolean mHasDataChanged = false;

/**
* Create an ObservableSnapshotArray where snapshots are parsed as objects of a particular
* class.
Expand Down Expand Up @@ -56,6 +58,9 @@ public ChangeEventListener addChangeEventListener(@NonNull ChangeEventListener l
for (int i = 0; i < size(); i++) {
listener.onChildChanged(ChangeEventListener.EventType.ADDED, get(i), i, -1);
}
if (mHasDataChanged) {
listener.onDataChanged();
}

return listener;
}
Expand All @@ -66,6 +71,11 @@ public ChangeEventListener addChangeEventListener(@NonNull ChangeEventListener l
@CallSuper
public void removeChangeEventListener(@NonNull ChangeEventListener listener) {
mListeners.remove(listener);

// Reset mHasDataChanged if there are no more listeners
if (!isListening()) {
mHasDataChanged = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

This block is a little surprising to me. Wouldn't you want the first listener attached to still fire immediately if the FirebaseArray contains data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@samtstern Yeah, that still happens. This just ensures that adding the first listener is the same whether or not this class was first created or is being reused. The contract for removeChangeEventListener is that the list must be cleared when there are no more listeners, as if the class was first created. Here's the FirebaseArray implementation:

// Clear data when all listeners are removed
if (!isListening()) {
    mQuery.removeEventListener((ValueEventListener) this);
    mQuery.removeEventListener((ChildEventListener) this);

    clearData();
}

}
}

/**
Expand Down Expand Up @@ -98,6 +108,7 @@ protected final void notifyChangeEventListeners(ChangeEventListener.EventType ty
}

protected final void notifyListenersOnDataChanged() {
mHasDataChanged = true;
for (ChangeEventListener listener : mListeners) {
listener.onDataChanged();
}
Expand Down