LiveQuery change listener not working properly?

I’ve read on docs that it’s better to have fewer views that can be queried in interesting ways, so I set up to reduce the number of views in my app.

There was a view that emitted a single value from documents with a specific type, which I decided I would eliminate because there was already a different view that emitted that same value (along with others).

So my approach was the following:
I wanted to show a group of documents to the user. The documents have a groupId propery which the old view used to emit. It emmited it as a key, with a null value.

Now I wanted to use a different view, one that I use for general search of documents and which emits multiple properties of a document, to get the groupId of the documents. The problem with this was that if I just queried the view for the specific groupId, there was the possibility to get a document that didn’t have that groupId, but did have the same groupId value in a different property that was emited by the view. This led me to set a static value on the emit of the groupId so that I could check for that value on the query. It looks like this:

  namesView = database.getView("general");
    if (namesView.getMap() == null) {
        namesView.setMap(new Mapper() {
            public void map(Map<String, Object> document, Emitter emitter) {
                if (document.get(TYPE) != null && document.get(TYPE).equals(REGISTRATION)) {

                    emitter.emit(document.get(NOMBRE), null);
                    emitter.emit(document.get(DORSAL), null);
                    emitter.emit(String.valueOf(document.get(GROUP_ID)), true);


However, when I do this, the query listener doesn’t get notified when there is a change to a document.
It does get notified though when there is a new document emited by the view (as in, when I create a document that has that same groupId)

Is there something that I’m missing here? Does this work as intended?

EDIT: I’m using couchbase Lite 1.4 for android

The LiveQuery notifies you when there’s a change to the query results … not on every change to a document. So if you change a document, but it doesn’t alter the values emitted by the map function, there’s no change and no notification.

Does that explain your issue? If not, can you give more details?

It does explain it, but: whenever I’ve used queries this way (emiting a null as a value) they have always triggered a document change regardless of the propery changed being emited or not. Is this working as intended?

There is a special case in the LiveQuery code to handle views that emit null … it treats the row as changed if the document that emitted it changed at all. It was added because a lot of people do that and expect it to work :slight_smile:

But if you emit anything else than null, the LiveQuery behaves as I described above.

In general it’s best to emit whatever document properties you’re going to be displaying (or otherwise working with.) Then you don’t have this problem, and also you can update your UI using just the view rows without having to also load the entire documents.