liveQuery remains alive

Hi, working on Android version 2.0 DB22.
I’m trying to create a live query that will be updated each time the changed() is called.
For example, every time the changed() is called, update the where expression to bring new documents that were added from now on (To avoid brining documents from last time).
My solution was to remove the query listner and add a new one but I see on the logs that the query remains alive and after each insertetion there is new line, so after 5 insertion I’ll get

I/LiteCore [SQL]: Created prerecorded query enum with 1 rows (Y bytes) in Xms

I/LiteCore [SQL]: Created prerecorded query enum with 2 rows (Y bytes) in Xms

I/LiteCore [SQL]: Created prerecorded query enum with 3 rows (Y bytes) in Xms

I/LiteCore [SQL]: Created prerecorded query enum with 4 rows (Y bytes) in Xms

Although the changed() function is called only once with the latestet document.

So, I tried only removing the old listneres but the query still runs (See it on logs) but no new lines are added.
This is a test code :

private void startliveQuery() {
       Expression whereExp = Expression.property(DB_UPDATE_TIME_COLUMN_NAME).greaterThan(Expression.longValue(lastSelectedTime));
       mQuery = QueryBuilder.select(SelectResult.all())
                       .from(DataSource.database(mDatabase))
                       .where(whereExp );
       mQueryListener = new myListener();
       mToken = mQuery.addChangeListener(mQueryListener);
   }
   
    public class myListener implements QueryChangeListener {

       String mUri;

       ReactiveLayerListener() {
           mUri = uriToBroadcast;
       }

       @Override
       public void changed(QueryChange queryChange) {

           ResultSet rows = queryChange.getResults();
           Result row;
   		//Do something with the results();
   		  mQuery.removeChangeListener(mToken);
   		mQueryListener = null;
   		lastSelectedTime = System.currentTimeMillis();
Expression where = Expression.property(DB_UPDATE_TIME_COLUMN_NAME).greaterThan(Expression.longValue(lastSelectedTime));
mQuery = somefunction(where ); //Using queryBuilder creates Query object
mToken = mQuery.addChangeListener(mQueryListener);
   	}
   }

My question is, how do I do it properly?
How do I create only one listener that will be updated and I won’t see old queries running on logs?

A query will automatically stop if there are no registered listeners .

Can you clarify a few things -

  • The somefunction(where) in your code snippet - Is that creating a new Query object ?
  • And what exactly is the issue that you are encountering ? Aren’t you getting documents corresponding to the updated query parameters ?

Also, have you considered parameterizing your query ? You can change query parameters without having to create a new query object - changing parameters will restart the query.

SomeFunction:
return QueryBuilder.select(SelectResult.all()).from(DataSource.database(mDatabase)) .where(where);

The issue that i am encoutring is that i see on the logs the old queries running. On the changed() function I do get only the latestet document but every time it is called I see another query on the logcat.
At the first time, I see only one log message. After changed() is called I see two query messages on the log, one with 2 results and one with one.
It doesn’t interfere with the app but If is changed will be called 100 times, there will be 100 queries to DB because I see it on the logs and we don’t want that - It means extra work! Somewhere the old queries are still alive! I want only one message on the log with the latest document.
So if you say that query can be changed with prameters, maybe that is the solution, how can I change the parametes where LAST_DB_TIME > system.currentMills() after each time the changed() is called ? (Each inserted document has LAST_DB_TIME field)

Interesting. One you removed the observers, query should stop. I will double check internally. Looks like you have a corresponding GitHub ticket as well.

Regarding params, check out this response .

Can you please update your queries to use params and test it out. That way, you don’t need to remove/add listeners or create a new query object . You can just change the params in you callback.

Thank you for your response.
I will inform you tomorrow

Hey - wondering if this is still an issue. Did parameterizing query fix it ?

Yes it did. Beware to check it tough.
A good documentation is required too on the parameters thing.
You should fix it for next release because the db may explode.

Check “what” though?

Yes. we have a ticket to update the docs before GA. But the blog should have helped - right ?

we are tracking why removal of listener didn’t stop the query.

Hi @itai.shalom2,

I wrote the test to reproduce the daemon LiveQuery issue. However, I can not reproduce this. Could you please write sample app to reproduce this issue?

Thanks!