Why We Should In­te­grate Couchbase Lite with An­droid Stu­dio

Couchbase Lite is an em­bed­ded NoSQL data­base for mo­bile de­vices that runs lo­cally on the de­vice with full CRUD and query ca­pa­bil­i­ties. In this ar­ti­cle, we dis­cover how to in­te­grate Couchbase Lite with An­droid Stu­dio.

OpenSource For You - - Contents - By: Shra­van I.V. The au­thor cur­rently works as a soft­ware de­vel­oper at Cisco Sys­tems In­dia and is in­ter­ested in open source tech­nolo­gies. You can reach him at

An­droid for mo­bile de­vices cur­rently comes with an in­built lo­cal data­base—SQLite. This is an RDBMS based light­weight data­base that is avail­able by de­fault on all An­droid op­er­at­ing sys­tems and pro­vides CRUD op­er­a­tions to ef­fi­ciently power your apps. SQLite is re­ally a great choice when the re­quire­ment is just to have a sim­ple data­base for your app to man­age struc­tured data. How­ever, when the need is to store semi-struc­tured or un­struc­tured data and also to han­dle com­plex queries at scale with­out wor­ry­ing about the schema of ta­bles, then SQLite may not suit all of the de­vel­oper’s re­quire­ments. A NoSQL data­base can be a bet­ter fit with these scal­ing re­quire­ments. Com­par­isons be­tween an SQL and a NoSQL data­base have fu­elled many de­bates, but both com­ple­ment each other rather than com­pete with each other.

In this ar­ti­cle, we start by dis­cussing the gen­eral data­base re­quire­ments for mo­bile de­vices, fol­lowed by NoSQL’s promi­nence in to­day’s mo­bile world and, fi­nally, look at in­te­grat­ing a NoSQL data­base called Couchbase Lite with An­droid Stu­dio.

Data­bases for mo­bile de­vices

De­cid­ing on a data­base for mo­bile de­vices re­quires us to con­sider var­i­ous fac­tors like mem­ory con­straints, the user ex­pe­ri­ence, a light­weight UI, etc – pa­ram­e­ters that are very dif­fer­ent com­pared to what would be re­quired for a desk­top or Web en­vi­ron­ment. So be­fore we jump into in­te­grat­ing Couchbase Lite with An­droid, let us first check out the var­i­ous re­quire­ments for data­bases in the mo­bile en­vi­ron­ment, which are listed be­low.

Un­like desk­tops or servers, mo­bile de­vices tend to have a lower bat­tery life and rel­a­tively slower CPUs. Data­bases should hence not be per­for­mance in­ten­sive and should be able to ef­fec­tively per­form fre­quent op­er­a­tions like searches and up­dates.

Mo­bile data­bases should have a smaller mem­ory foot­print. In cer­tain ways, higher mem­ory re­quire­ments would also lead to in­creased CPU cy­cles, where the ker­nel tries to in­ten­sively search for avail­able mem­ory space in the RAM. Lower foot­print de­mands not only lead to lower CPU cy­cles but also en­sure other mo­bile apps don’t get im­pacted.

All the win­ning mo­bile apps are high per­for­mance with a fast loading time. Apps that freeze con­stantly are al­ways on the back­bench. Data con­sis­tency is an­other re­quire­ment when talk­ing about these lo­cal data­bases on the mo­bile. If one were to go with a dis­trib­uted data­base, the data may be­come in­con­sis­tent with re­spect to its re­mote coun­ter­part if not taken care of, and the de­vice might even dis­cover this only on con­nect­ing to the In­ter­net.

A mo­bile de­vel­oper with a cloud based data­base back­end, like say Fire­base, needn’t worry about most of these con­straints and re­quire­ments, but needs to in­clude these fac­tors in the equa­tion when opt­ing for a lo­cal data­base.

NoSQL’s rel­e­vance in the mo­bile world

With the in­creased us­age of mo­bile de­vices, a tremen­dous amount of data is be­ing gen­er­ated these days. This fact, clubbed with tech­nol­ogy pro­lif­er­a­tion into new spa­ces like mo­bil­ity, IoT and an­a­lyt­ics, has led to a de­mand for mo­bile de­vices and apps to han­dle this high vol­ume of data at a high speed. Be­sides, the na­ture of data (es­pe­cially when it comes from IoT de­vices for which data ex­change is in real-time) is con­tin­u­ous and ei­ther semi-struc­tured or the ap­pli­ca­tion needs to cater or adapt to var­i­ous schemas. Some of the fun­da­men­tal philoso­phies that NoSQL brings in ad­dress these chal­lenges in the mo­bile space, as dis­cussed be­low.

The very fact that NoSQL is schema-less will help de­vel­op­ers han­dle the data that lacks schema or struc­ture. Be­sides, this prop­erty will also let them scale to chang­ing or evolv­ing re­quire­ments of data. The change in schema or struc­ture could be done with ease at any point in time, in an in­de­pen­dent way, with­out af­fect­ing the ex­ist­ing code. All of this will di­rectly re­sult in the ag­ile de­liv­ery of apps, a quick turn­around time to mar­ket as against

the time con­sum­ing process of de­sign con­sid­er­a­tions, and limited scope for scal­a­bil­ity and mod­u­lar­ity in code when us­ing a re­la­tional data­base.

The dis­trib­uted ar­chi­tec­ture of NoSQL data­bases en­sures that they per­form bet­ter than RDBMS. Be­sides, NoSQL doesn’t have com­plex join op­er­a­tions and nor­malised data nor does it in­clude com­plex queries. These fac­tors give it the up­per hand when it comes to data­base per­for­mance. Ef­fec­tive per­for­mance di­rectly re­sults in the bet­ter user ex­pe­ri­ence of mo­bile apps be­cause of the re­duced load time of UI com­po­nents and ac­tiv­i­ties. This di­rectly im­proves the bat­tery life too.

Se­cu­rity is an­other as­pect that should never be ig­nored while try­ing to achieve all these goals. The data­bases should be able to com­mu­ni­cate to the server over se­cure chan­nels. Be­sides, the chan­nels that com­mu­ni­cate with mo­bile de­vices over the In­ter­net de­mand low la­tency for an im­proved mo­bile user ex­pe­ri­ence. Also,ex­change of data on the net­work should be light­weight to meet these per­for­mance re­quire­ments.

All that said, we still may not be able to elim­i­nate SQL data­bases which com­ple­ment NoSQL in many ways. NoSQL doesn’t guar­an­tee atom­ic­ity or in­tegrity of data which an RDBMS is ca­pa­ble of. So, it is the de­vel­oper’s needs at the end of day that de­cide which data­base to go with.

In­te­grat­ing Couchbase Lite with An­droid Stu­dio

Couchbase Lite is an open source project avail­able un­der Apache Li­cense 2.0. It is an em­bed­ded JSON data­base that can work as a stand­alone, in a P2P net­work, or as a re­mote end­point for a Sync Gate­way. In this ar­ti­cle, we ex­plain how to power your An­droid apps with Couchbase Lite.

Be­fore get­ting onto in­te­gra­tion, let us check out a few key fea­tures about this data­base.

Couchbase Lite stores and man­ages all the data lo­cally on your mo­bile de­vice in a light­weight JSON for­mat. Con­sid­er­ing the re­quire­ments for a mo­bile data­base, Couchbase Lite qual­i­fies with its lower mem­ory foot­print, built-in se­cu­rity with user au­then­ti­ca­tion, AES based data based en­cryp­tion, and trans­port to the server through TLS. Couchbase Lite pro­vides CRUD and query sup­port through na­tive APIs, and also works well with ex­ist­ing REST ar­chi­tec­tures with its pro­gram­matic ac­cess through REST API. Stream and batch APIs from Couchbase Lite en­able the trans­fer of real-time data in batches with low net­work la­tency and through­put, thereby ad­dress­ing the ex­act de­mands of mo­bile apps.

Let us now go through the steps of in­stalling Couchbase Lite and the other ba­sic op­er­a­tions to get started.

It is as­sumed that read­ers are al­ready con­ver­sant with the An­droid Stu­dio IDE for de­vel­op­ing An­droid apps. In­te­grat­ing Couchbase dB with An­droid is straight­for­ward. You could start off by adding the de­pen­dency el­e­ments given be­low in your ap­pli­ca­tion’s build.gra­dle:

de­pen­den­cies { com­pile 'com.couchbase.lite:couchbase-lite-an­droid:+'


In the Java part of the ap­pli­ca­tion, you would need the fol­low­ing ba­sic set of pack­ages to start with:

im­port com.couchbase.lite.*; im­port com.couchbase.lite.an­droid.An­droidCon­text;

Now that you are all set to use CouchBase APIs in your An­droid app, I’d like to il­lus­trate a sam­ple code, used for cre­at­ing a data­base, and do an in­sert, up­date and delete of a doc­u­ment in it as men­tioned in (1),(2), (3) and (4), re­spec­tively.

// (1) Get the data­base or cre­ate it if it doesn't al­ready ex­ist.

Man­ager man­ager = new Man­ager(new JavaCon­text(),Man­ager. DEFAULT_OPTIONS);

Data­base db = man­ager.getDatabase("couchdB");

// (2) Cre­ate a new doc­u­ment (a record) in the data­base. Doc­u­ment doc = db.cre­ateDoc­u­ment();

Map prop­er­ties = new HashMap(); prop­er­ties.put("firstName", "OSFY"); doc.putProp­er­ties(prop­er­ties);

// (3) Up­date a doc­u­ment. doc.up­date(new Doc­u­ment.Doc­u­men­tUp­dater() {

@Over­ride pub­lic boolean up­date(Un­savedRe­vi­sion newRe­vi­sion) { Map prop­er­ties = newRe­vi­sion.getUserProp­er­ties(); prop­er­ties.put("firstName", "Johnny"); newRe­vi­sion.setUserProp­er­ties(prop­er­ties); re­turn true;



// (4) Delete a doc­u­ment. doc.delete();

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.