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

Commit ad4a554

Browse files
author
Hideki Itakura
authored
Fixed #1963 - DB023 - Crazy amount of dbListenerTokens - makes the DB 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.
1 parent 028ce79 commit ad4a554

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

shared/src/main/java/com/couchbase/lite/LiveQuery.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ void start() {
119119
observing = true;
120120
releaseResultSet();
121121
query.getDatabase().getActiveLiveQueries().add(this);
122-
dbListenerToken = query.getDatabase().addChangeListener(this);
122+
// NOTE: start() method could be called during LiveQuery is running.
123+
// Ex) Query.setParameters() with LiveQuery.
124+
if(dbListenerToken == null)
125+
dbListenerToken = query.getDatabase().addChangeListener(this);
123126
update(0);
124127
}
125128
}
@@ -131,8 +134,10 @@ void stop(boolean removeFromList) {
131134
synchronized (lock) {
132135
observing = false;
133136
willUpdate = false; // cancels the delayed update started by -databaseChanged
134-
if (query != null && query.getDatabase() != null)
137+
if (query != null && query.getDatabase() != null && dbListenerToken != null) {
135138
query.getDatabase().removeChangeListener(dbListenerToken);
139+
dbListenerToken = null;
140+
}
136141
if (removeFromList && query != null && query.getDatabase() != null)
137142
query.getDatabase().getActiveLiveQueries().remove(this);
138143
releaseResultSet();

0 commit comments

Comments
 (0)