Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Fixed #1963 - DB023 - Crazy amount of dbListenerTokens - makes the DB…
Browse files Browse the repository at this point in the history
… run slow (#1694)

- LiveQuery with `Query.setParameters(...)` calls `LiveQuery.start()`. In `start()` method, registering Database Listener every time. If already Database Listener is registered, it is not necessary to new one.
  • Loading branch information
Hideki Itakura authored Apr 2, 2018
1 parent 028ce79 commit ad4a554
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions shared/src/main/java/com/couchbase/lite/LiveQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ void start() {
observing = true;
releaseResultSet();
query.getDatabase().getActiveLiveQueries().add(this);
dbListenerToken = query.getDatabase().addChangeListener(this);
// NOTE: start() method could be called during LiveQuery is running.
// Ex) Query.setParameters() with LiveQuery.
if(dbListenerToken == null)
dbListenerToken = query.getDatabase().addChangeListener(this);
update(0);
}
}
Expand All @@ -131,8 +134,10 @@ void stop(boolean removeFromList) {
synchronized (lock) {
observing = false;
willUpdate = false; // cancels the delayed update started by -databaseChanged
if (query != null && query.getDatabase() != null)
if (query != null && query.getDatabase() != null && dbListenerToken != null) {
query.getDatabase().removeChangeListener(dbListenerToken);
dbListenerToken = null;
}
if (removeFromList && query != null && query.getDatabase() != null)
query.getDatabase().getActiveLiveQueries().remove(this);
releaseResultSet();
Expand Down

0 comments on commit ad4a554

Please sign in to comment.