Performance and best practice on couchbase lite 2.5

Hi everybody,
I would like to open a discussion about what is the best practice to use the query on an android application and evaluate several aspect in term of performance.

In the example the query is built on the fly then executed it,

There is any impact on the performance creating the query every time, or is it better save it in a singleton object and reuse it every time? What if I have a to attach a listener to a query (old live query 1.4)?

In our mobile application we use to attach to every document a listener, this because the code is been migrating from couchbase lite 1.4 to 2.5 and we might have thousand of documents.
Now the new library does not allow to attach listener to the document itself, but on the database, to a particular document id [addDocumentChangeListener] but now there is also the listener to the whole database [addChangeListener]
What is the best practice to deal with this kind of issue? Having thousand of document which approach give the best performance?

Thanks for any feedback, I would like to have your opinion and if you had the same kind of issues in your projects.

-Paolo

Creating a query is relatively expensive so you’d get a benefit from saving queries around. If you need them to be a bit more flexible you can introduce query variables into the query statement.

The rest is unclear what you are actually trying to do. Why is it that you need to listen to every document?

Now the new library does not allow to attach listener to the document itself, but on the database, to a particular document id

It’s the same functionality, the API is just a little bit different.

Adding listeners to a lot of documents can get expensive. It’s better to add a database listener and then just look at the document ID in the notification.

Sorry if I reply just now. I took few days off.
Thanks for both the replies.
I used a listener for every document because I would like to apply the MVC design pattern, so the model is based on the documents in the Database, I have multiple views (App Side and Server Side) when one of them change a document I want an event to update the views in real time.
Coming from Couchbase Lite 1.4.4 I use to have a listener for each document, for sure, have the list of changed ids in one single listener it would be much better (also because I might have 1k documents alive)

About the Query, Do you mean to use the setParameters(Parameters parameters) ?

Another point I would like to discuss is the use of the Indexes in the Android APP. Looking to the documentation, it seems it is something to create at the beginning, and then leave there. But it is something I can use? Like in the Full Text search example here?
https://docs.couchbase.com/couchbase-lite/current/java.html#indexing
Because it looks like something under the hood that improves the performance but I don’t really have a clear idea about how it works and if I need them.

Thanks again for any feedback.

It’s fine to have a listener for every document that’s showing up in your UI, since that’s probably a fairly small number. I was warning against having very large numbers (thousands…) of them since it will affect performance. In that case you’re better off with a single database listener, whose implementation can key off the docID.

About the Query, Do you mean to use the setParameters(Parameters parameters) ?

Yes. Make the changeable parts of a query parameters, then you can update them and re-run the query without having to recompile it.

it looks like something under the hood that improves the performance but I don’t really have a clear idea about how it works and if I need them.

Regular indexes and full-text-search indexes are slightly different. Regular indexes are just something you create once and leave there, and their only effect is to make queries faster. (If you’ve used any SQL databases, the functionality is exactly the same.)