phonegap android: moving javascript views to java

266 views
Skip to first unread message

Daniel Carr

unread,
Oct 17, 2013, 7:34:54 PM10/17/13
to mobile-c...@googlegroups.com
Hi all.

I'm currently moving an app from coucdb to couchbase-lite. The app is implimented in phonegap. 

If I rewrite a view's map and reduce functions in java, where do they go? Can I just modify the appName.java file and use the setMapReduceBlocks() function, ( as described here http://docs.couchbase.com/couchbase-lite/cbl-android/#getting-started ) ? (if so, any pointers or examples on this? I'm pretty much learning java to do this)

Or, is it possible to write a design doc with laguage: java and the functions in that design doc somehow, and use the http api to put the design docs in a couchbaseLite database?

Cheers,
Daniel

Traun Leyden

unread,
Oct 18, 2013, 1:40:39 PM10/18/13
to mobile-c...@googlegroups.com
If you are using Phonegap, you'll only be able to use views written in Javascript.



--
You received this message because you are subscribed to the Google Groups "Mobile Couchbase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jens Alfke

unread,
Oct 18, 2013, 2:33:18 PM10/18/13
to mobile-c...@googlegroups.com

On Oct 18, 2013, at 10:40 AM, Traun Leyden <traun....@gmail.com> wrote:

If you are using Phonegap, you'll only be able to use views written in Javascript.

Couldn’t you use a Java view if you registered its callback in the wrapper code at launch time?
(It’s less convenient, but it would run faster.)

—Jens

Traun Leyden

unread,
Oct 18, 2013, 2:41:45 PM10/18/13
to mobile-c...@googlegroups.com
Yeah, let me qualify my statement by saying that "currently, there is no clean way to use non-Javascript views in a Phonegap app"  Jens what you said should theoretically work, but that would involve some hacking and the idea is that people writing phonegap apps wouldn't be messing with the wrapper code.  

If that became a common requirement, we'd want to provide a clean way to do it where it didn't require recompiling the wrapper code.  (eg, make it pluggable)


--

Daniel Carr

unread,
Oct 20, 2013, 8:16:57 PM10/20/13
to mobile-c...@googlegroups.com
Thanks for your replies.

If anyone is keen to try this, it's pretty easy. I know very little java and was able to work it out, and the performance is way better.


To get it to work, clone the git repo and import the modified cloned repo as a phonegap plugin:

git clone https://github.com/couchbaselabs/Couchbase-Lite-PhoneGap-Plugin.git
cd project
phonegap local plugin add ../Couchbase-List-PhoneGap-Plugin

Every time you modify the plugin, you need to remove and add it again.

phonegap local plugin remove com.couchbase.lite.phonegap
phonegap local plugin add ../Couchbase-List-PhoneGap-Plugin


As a proof of concept, I edited /src/android/CBLite.java file in Cordova-Lite-PhoneGap-Plugin.

Add these to the imports:

import com.couchbase.cblite.CBLDatabase;
import com.couchbase.cblite.CBLViewMapBlock;
import com.couchbase.cblite.CBLViewMapEmitBlock;

Remove the Javascript View thingy:

-                       CBLView.setCompiler(new CBLJavaScriptViewCompiler());





e.g.: (sorry about the dodgy tabulation)


@@ -41,12 +45,25 @@ public class CBLite extends CordovaPlugin {
 
                        CBLURLStreamHandlerFactory.registerSelfIgnoreError();
 
-                       CBLView.setCompiler(new CBLJavaScriptViewCompiler());
-
                        String filesDir = this.cordova.getActivity().getFilesDir()
                                        .getAbsolutePath();
                        CBLServer server = startCBLite(filesDir);
-
+                       
+                       CBLDatabase db = server.getDatabaseNamed("treatment");
+                       CBLView testView = db.getViewNamed("ivetTest/test");
+                       testView.setMapReduceBlocks(new CBLViewMapBlock() {
+
+                   @Override
+                   public void map(Map<String, Object> document, CBLViewMapEmitBlock emitter) {
+                       String type = (String)document.get("Type");
+                       if( "TreatmentRecord".equals(type) ) {
+                           emitter.emit( document.get("_id"), document.get("Location") );
+                                       
+                       }
+
+                   }
+               }, null, "1.1"); 
+                       
                        listenPort = startCBLListener(DEFAULT_LISTEN_PORT, server);
 
                        System.out.println("initCBLite() completed successfully");

Traun Leyden

unread,
Oct 21, 2013, 1:58:38 PM10/21/13
to mobile-c...@googlegroups.com
Thanks for contributing this!


Ami Gandhi

unread,
Feb 13, 2014, 9:50:28 AM2/13/14
to mobile-c...@googlegroups.com
Hi!
Can you tell me how to query CBLView from phonegap android?
Also, how to set startkey and endkey?

Traun Leyden

unread,
Feb 13, 2014, 12:51:18 PM2/13/14
to mobile-c...@googlegroups.com

I think there should be examples of that in https://github.com/couchbaselabs/TodoLite-PhoneGap.

It should essentially be the same as making a view query via REST to CouchDB, so you can also check the CouchDB docs.

@Chris -- I guess the recommended js library for talking to Couchbase Lite from javascript is "coax/hoax" right?  Is there any others that people should be aware of that will make their lives easier?  
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.

Ami Gandhi

unread,
Feb 13, 2014, 2:15:50 PM2/13/14
to mobile-c...@googlegroups.com
Hi!

Thanks for your response!
I am using this method only.

Currently I am defining view like this from Phonegap:
get_contacts:
{
map:  function(doc) {
if (doc.type && doc.is_active && doc.type == "contact")
{
emit(doc.type,doc)
}
}.toString()
}

But calling view from Phonegap is very slow!
Having this logs all the time.
02-14 00:45:05.685: D/dalvikvm(6462): WAIT_FOR_CONCURRENT_GC blocked 14ms
02-14 00:45:05.715: V/CBLDatabase(6462):   call map for sequence=14
02-14 00:45:05.750: D/dalvikvm(6462): GC_CONCURRENT freed 492K, 27% free 13190K/17863K, paused 2ms+2ms, total 23ms

So, moving on native approach.

CBLDatabase db = server.getDatabaseNamed("jn_ami");
CBLView testView = db.getViewNamed("amiview");
testView.setMapReduceBlocks(new CBLViewMapBlock() {
@Override
public void map(Map<String, Object> document, CBLViewMapEmitBlock emitter) {
if(document.get("type").equals("contact"))
{
emitter.emit(document.get("type"), null);
}
}
}, null, "1.1");
            
But it is not working when I call view "amiview" from phonegap.
So, any solution for this?
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchbase+unsubscribe@googlegroups.com.

J. Chris Anderson

unread,
Feb 14, 2014, 2:17:37 PM2/14/14
to mobile-c...@googlegroups.com


On Thursday, February 13, 2014 9:51:18 AM UTC-8, Traun Leyden wrote:

I think there should be examples of that in https://github.com/couchbaselabs/TodoLite-PhoneGap.

It should essentially be the same as making a view query via REST to CouchDB, so you can also check the CouchDB docs.

@Chris -- I guess the recommended js library for talking to Couchbase Lite from javascript is "coax/hoax" right?  Is there any others that people should be aware of that will make their lives easier?  


Coax is a fine way to make http calls, but at the end of the day, if someone is comfortable calling $.ajax() or using any other XHR library, they are all equivalent as far as CBL is concerned.


Chris

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

Traun Leyden

unread,
Feb 14, 2014, 7:35:42 PM2/14/14
to mobile-c...@googlegroups.com


On Thursday, February 13, 2014, Ami Gandhi <ami....@gmail.com> wrote:


But calling view from Phonegap is very slow!
Having this logs all the time.
02-14 00:45:05.685: D/dalvikvm(6462): WAIT_FOR_CONCURRENT_GC blocked 14ms
02-14 00:45:05.715: V/CBLDatabase(6462):   call map for sequence=14
02-14 00:45:05.750: D/dalvikvm(6462): GC_CONCURRENT freed 492K, 27% free 13190K/17863K, paused 2ms+2ms, total 23ms

Have you taken a look at the Android Monitor / DDMS to see where the memory is being used?
 

So, moving on native approach.

Did you follow the instructions from Daniel Carr above?  Did you comment out the call to "CBLView.setCompiler(new CBLJavaScriptViewCompiler());"

Another thing that could be going wrong, are you defining a view with the exact same name for the javascript based view you defined?  I would recommend using a different name so they don't conflict.


            
But it is not working when I call view "amiview" from phonegap.

Is there an error message?


 
--
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/bc488580-9fab-4f22-8a51-a0bc697e931f%40googlegroups.com.

Ami Gandhi

unread,
Mar 17, 2014, 10:49:31 AM3/17/14
to mobile-c...@googlegroups.com
I have solved this issue by this:

Android Code: To create view.

Database db = server.getDatabase(strDbName);
db.open();
listenPort = startCBLListener(DEFAULT_LISTEN_PORT, server);

View v1 = db.getView(String.format("%s/%s",
strDbName,
                        "getrecords_typewise"));

//Here strDbName = database name, "getrecords_typewise" = View name.

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(new Object[]{docType.toString()}, document);
        }
}
}, strVersion);
db.registerView(v1);

Phonegap  Code:
config.views(["getrecords_typewise", {
   startkey : ["contact"],
            endkey : ["contact"],
   descending : true
   }], function(err, view)
   {
var len = view['rows'].length;
   var counter=0;
   var data=[];
   if(view['rows']!=null && view['rows'].length>0){
   for(j=0;j<view['rows'].length;j++)
   {
                              data.push(view['rows'][j].value);
                     }
               }
          });


--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/lAt7w3ntbKo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/CACSSHCHskg2tQ2ce8TckCh0j0PFZ314aidUP6UfztFhn2%3DqwVg%40mail.gmail.com.

Ami Gandhi

unread,
Mar 17, 2014, 10:50:26 AM3/17/14
to mobile-c...@googlegroups.com

Hi!

I am using kendo UI listview for listing data in my Phonegap application.
For android, I set hardware acceleration="false" in my android manifest file.
< application android:hardwareAccelerated="false" >
It made a huge difference in performance.
How can I do this in iOS?

Jens Alfke

unread,
Mar 17, 2014, 11:15:31 AM3/17/14
to mobile-c...@googlegroups.com

On Mar 17, 2014, at 7:50 AM, Ami Gandhi <ami....@gmail.com> wrote:

> I am using kendo UI listview for listing data in my Phonegap application.
> For android, I set hardware acceleration="false" in my android manifest file.
> < application android:hardwareAccelerated="false" >
> It made a huge difference in performance.
> How can I do this in iOS?

This isn’t a Couchbase Lite question, so it’s not appropriate for this list.

Also, there’s no need to disable hardware acceleration on iOS, nor any way to do it. iOS graphics work very differently from Android’s, and are generally a lot faster.

—Jens

Ron Williams

unread,
Feb 1, 2015, 3:03:29 AM2/1/15
to mobile-c...@googlegroups.com
I hate to bump an old thread but i'm looking to achieve the same thing here. Are there examples out there of making java views accessible to the http api for phonegap style apps? I read through these posts and have had no success getting it to work.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchbase+unsubscribe@googlegroups.com.

Ron Williams

unread,
Feb 2, 2015, 5:25:07 AM2/2/15
to mobile-c...@googlegroups.com
Disregard my last post. I got it working. 
Reply all
Reply to author
Forward
0 new messages