LiveQuery after each new document?

64 views
Skip to first unread message

Abdolhosein V. Ebrahimi

unread,
Jan 1, 2015, 11:59:51 AM1/1/15
to mobile-c...@googlegroups.com
Is livequery performed after adding every new document?
I have a db with a million records:
It's somehow annoying because line A (check below) is called million times after updating just a single record:


liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
                public void changed(final LiveQuery.ChangeEvent event) {
                    int c=0;
                    for (Iterator<QueryRow> it = event.getRows(); it.hasNext(); ) {
                        c++;
// LINE A
                        Log.d("SSS", String.valueOf(c));
                        final QueryRow row = it.next();
                        SavedRevision currentRevision = row.getDocument().getCurrentRevision();
                        activity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                liveQueryEvent.newRowDroppedFromSky(row);
                            }
                        });

                    }
                }
            });

Jens Alfke

unread,
Jan 1, 2015, 2:11:15 PM1/1/15
to mobile-c...@googlegroups.com
On Jan 1, 2015, at 8:59 AM, Abdolhosein V. Ebrahimi <vakil...@gmail.com> wrote:

Is livequery performed after adding every new document?

It's called at some point after one or more new documents are added; the details are implementation-dependent. I don't know the details of the Java implementation; maybe Traun can give details. On iOS I recently optimized it so it won't fire more than (IIRC) twice a second.

Also, a transaction is treated as one change. So if you group a lot of updates into one transaction (which you should anyway for performance) the live query will update only once after the transaction commits.

I have a db with a million records:
It's somehow annoying because line A (check below) is called million times after updating just a single record:

Yes, it's not efficient to run a live query that returns such a large number of rows.

In your case it looks like you're only interested in new (or changed) documents? If so, it's much more efficient to listen for DatabaseChanged notifications instead of re-querying the whole database.

—Jens

Traun Leyden

unread,
Jan 8, 2015, 7:02:26 PM1/8/15
to mobile-c...@googlegroups.com

It's called at some point after one or more new documents are added; the details are implementation-dependent. I don't know the details of the Java implementation; maybe Traun can give details. On iOS I recently optimized it so it won't fire more than (IIRC) twice a second.


Yes, there is some logic in place that tries to prevent it from stacking up concurrent livequeries if one is running while a database update comes in from the db change listener.


Abdolhosein V. Ebrahimi

unread,
Jan 10, 2015, 9:00:54 AM1/10/15
to mobile-c...@googlegroups.com
So the scenario would be a liveQuery at app startup, then stop the liveQuery and just monitor replication changes?

Jens Alfke

unread,
Jan 10, 2015, 2:01:29 PM1/10/15
to mobile-c...@googlegroups.com

On Jan 10, 2015, at 6:00 AM, Abdolhosein V. Ebrahimi <vakil...@gmail.com> wrote:

So the scenario would be a liveQuery at app startup, then stop the liveQuery and just monitor replication changes?

Just run a regular Query. There's no reason to make it live if you're not going to use the auto-updating.

—Jens
Reply all
Reply to author
Forward
0 new messages