Live query in Phonegap

124 views
Skip to first unread message

Ami Kapadia

unread,
Apr 14, 2014, 3:56:22 AM4/14/14
to mobile-c...@googlegroups.com
Hi!

I am using CBL in my Phonegap application. I am creating native views (Android and iOS).
View v1 = db.getView(String.format("%s/%s",
strDbName,
                        "getrecords_typewise"));
v1.setMap(new Mapper() 
{
@Override
public void map(Map<String, Object> document, Emitter emitter) 
{
Object docType = document.get("type");
if (docType != null && 
document.get("is_active") != null 
&& document.get("is_active").equals(true))
{
            emitter.emit(docType.toString(), document);
               }
}
}, strVersion);
db.registerView(v1);

Then I am querying views from JavaScript like this :
 config.views(["getrecords_typewise", {
startkey : "contact",
        endkey : "contact",
descending : true
   }], function(err, view)
   {
   var len = view['rows'].length;
});

I want to use live queries and on dbChange, I will update my associated UI list. How can I do this?

Jens Alfke

unread,
Apr 14, 2014, 1:22:22 PM4/14/14
to mobile-c...@googlegroups.com

On Apr 14, 2014, at 12:56 AM, Ami Kapadia <ami....@gmail.com> wrote:

I want to use live queries and on dbChange, I will update my associated UI list. How can I do this?

Live queries are part of the native API, not the REST API. The equivalent is to read the _changes feed in longpoll or continuous mode, and re-run the query when new changes arrive. (Maybe some of the JS wrapper libraries have higher-level support for this, something like CBLLiveQuery? I’m not familiar with them.)

—Jens

J. Chris Anderson

unread,
Apr 14, 2014, 1:43:16 PM4/14/14
to mobile-c...@googlegroups.com
The trick is to rerun your query whenever the database changes. To do this you can connect to the _changes API via the longpoll query: http://docs.couchbase.com/couchbase-lite/cbl-api/#get-db-changes

Here is a place where I do it in code, by calling a changes API handler that I wrote.


Once I am listening to the feed, I use it to call a global function (window.onChange). My application swaps out that function depending on which view is active.

If you want that module (coax) it is available on npm, or you can grab the modules.js file used by TodoLite-PhoneGap, and require it like this: https://github.com/couchbaselabs/TodoLite-PhoneGap/blob/master/js/index.js#L21


Here is the readme for coax: https://github.com/jchris/coax

Hope that helps.

Ami Kapadia

unread,
Apr 14, 2014, 2:04:02 PM4/14/14
to mobile-c...@googlegroups.com
Hi J.Chris Anderson,
Thanks for your response.

Yes, I am using this API. I am doing synchronization at the time of login. There can be thousands of records to be synced.
In that case, connectToChanges and window.dbChanged() will be called thousand times.

So, If I write logic of refreshing list in window.dbChanged(), it will refresh list thousand times.
Can you tell me is there any flag which tells that synchronization is now done. So, I can refresh list there.

Also, How could I know which list is to update at which time. Suppose I am having 5 views. So, every time after sync I have to update all 5 lists?

Thanks!
Ami


--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/e8a3bf27-c58c-4f56-b777-d138f5f44faf%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

J. Chris Anderson

unread,
Apr 14, 2014, 5:40:59 PM4/14/14
to mobile-c...@googlegroups.com
The easiest thing is to look at the first argument to the dbChanged function, which will be the document itself. From that you can see if the document would impact the user's current screen, and redraw conditionally.

As far as ignoring the first set of records, you can request the /mydatabase/ url to get the current local sequence number, if you pass that into the changes connection as a since parameter, it will start from there, skipping any older changes.

The implementation of the since handling behavior is here: https://github.com/jchris/coax/blob/master/lib/coax.js#L74 It is also used so that subsequent changes requests start from the correct sequence. The continuous mode should only be used on server-side js due to XHR limitations, so I use longpoll by default in coax.

As far as waiting until first sync is complete, you can parse the output of the _active_tasks API, like I do here: https://github.com/couchbaselabs/TodoLite-PhoneGap/blob/master/js/index.js#L797

Chris

To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchbase+unsubscribe@googlegroups.com.

Jens Alfke

unread,
Apr 14, 2014, 8:27:17 PM4/14/14
to mobile-c...@googlegroups.com
On Apr 14, 2014, at 11:04 AM, Ami Kapadia <ami....@gmail.com> wrote:

So, If I write logic of refreshing list in window.dbChanged(), it will refresh list thousand times.

It’s better to put in a delay so you don’t refresh the list more often than once or twice a second.

Can you tell me is there any flag which tells that synchronization is now done. So, I can refresh list there.

Are you sure you want to wait that long? If syncing takes a while, your list would be out of date with the database.

—Jens

Ami Kapadia

unread,
Apr 15, 2014, 6:52:57 AM4/15/14
to mobile-c...@googlegroups.com
Thanks for quick response:
I can move forward now but few query for now.
See in-line

On Tue, Apr 15, 2014 at 5:57 AM, Jens Alfke <je...@couchbase.com> wrote:

On Apr 14, 2014, at 11:04 AM, Ami Kapadia <ami....@gmail.com> wrote:

So, If I write logic of refreshing list in window.dbChanged(), it will refresh list thousand times.

It’s better to put in a delay so you don’t refresh the list more often than once or twice a second.
Yes, It's good. Here is the logic which I am writing.
My applications have 3 main list and many sub list.
config.db.changes({since : config.info.update_seq}, function(err, change){
    if(change == undefined || change == null)
  {
  lastSeq = 0;
   }
  else
  {
  lastSeq = change.seq
  $("#lblSyncStatus").text("Records sync : " + lastSeq);
//I can fetch document id, rev and seq here
//So I am fetching each doc from CBL and checking it's type, and there is some delay logic and updating associated liist which worked, I will move this logic to window.dbChanged later on
  }  
   window.dbChanged();
  })

Can you tell me is there any flag which tells that synchronization is now done. So, I can refresh list there.

Are you sure you want to wait that long? If syncing takes a while, your list would be out of date with the database.
Yes, on login time (first time when user installed application), I am first downloading all documents and then I want to create native views and calling it from JS, After finishing it I navigate to main screen of App which have list. All codes are already ready and working well after first pull. So I just want to know when first pull finished.

—Jens
Thanks, 
Ami 

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages