By synchronizing data from the server to the phone and working offline, Couchebase Mobile may be the best solution available today:

Source: Internet
Author: User
Tags couchbase name database cloudant

By synchronizing data from the server to the phone and working offline, Couchebase Mobile may be the best solution available today:

Original address:

Https://www.infinum.co/the-capsized-eight/articles/server-client-syncing-for-mobile-apps-using-couchbase-mobile

If you ' re developing a content rich application this synchronizes data from server to smartphone and needs to work offline The article for you.

Every once in a while, your end up working on a project this throws you out of your comfort zone and requires some heavy du Ty Learning.

For me, it is a seemingly simple project this required things like data replication and high availability, things I vague Ly remember from my college days. After some exploring, I came across Couchbase Mobile, the A framework that offers support for Mobile app development and cover s all of the requirements.

Couchbase Mobile has major parts:

    • Couchbase Lite-an Embedded, schemaless, JSON database
    • Sync gateway-a mechanism to sync data to and from the server

Nosql

Couchbase Lite is a NOSQL database, which means that there ' s no schema or a predefined data structure. It's a document store and the documents are JSON objects. It also means that you don't need to worry about structural changes in the data, since there ' s little structure to begin W ith. This allows for great flexibility and little maintenance.

Views

If you need to build reports, aggregate and join documents, or has different representations of the data, you can use Vie Ws. Views is defined in JavaScript. They is built dynamically and don ' t affect the underlying data, so can has as many as you like.

Distribution

Couchbase ' s distribution system is incredibly complex and very powerful. It has the several main characteristics, and you can fine tune your app to use any or all of them:

    • Master→slave replication
    • Master↔master replication
    • Filtered Replication
    • Incremental and Bi-directional replication
    • Conflict Management
NETWORK availability

Additionally, you don ' t need-care for the changes in the network availability. The underlying network listener in the library monitors the changes, and pauses and resumes whatever replication you have Running, leaving you ample space to notify the user's the current network status.

Replication itself can be one-shot or continuous. If you want to say when and which needs to is synced to or from the host, you'll use one-shot replication. On the other hand, if the requirements say that data should is synced anytime a change occurs and then continuous replication Is the the-the-go. Both replications would download the same data, but keeping continuous replication running requires minimal data traffic, I n my case less than kb/h.

How does I implement it?

After we've covered the basics, let's see just how simple setting up a app with Couchbase Lite is. The official Getting Started pages is quite detailed and easy to follow. In the following example, I use the Cloudant service as the backend for my demo application, but can setup your own ho St with CouchDb on it.

Here are a code example of the bare minimum needed to implement bidirectional replication from your Android application;

1. Add the repository location to the application ' s root build.gradle file
Buildscript {    repositories {        mavencentral ()        maven {            url "http://files.couchbase.com/maven2/"        }        mavenlocal ()    }    dependencies {        classpath ' com.android.tools.build:gradle:0.9.+ '    }}allprojects {    repositories {        Mavencentral ()        maven {            url "http://files.couchbase.com/maven2/"}}    }
2. Add the dependency to the module ' s Build.gradle file
Compile ' com.couchbase.lite:couchbase-lite-android:1.0.0 '
3. Add the following code to your application ' s main activity or fragment
Initialise the database protected void Initdb () throws IOException, Couchbaseliteexception {//Create the Databa SE Manager with default options Manager Manager = new manager (new Androidcontext (mainactivity.this), Manager.defaul        T_options);        Get or create the database with the provided name database = Manager.getdatabase ("Demodb");    Add a Change listener database.addchangelistener (Databaselistener);        }//start bi-directional syncingprotected void Startsync () {URL syncurl;        try {syncurl = new URL ("Https://username:password" + "@username. Cloudant.com/demodb");        } catch (Malformedurlexception e) {throw new RuntimeException (e);        }//server-client Replication pullreplication = database.createpullreplication (Syncurl);        Pullreplication.setcontinuous (TRUE); Client-server Replication pushreplication = database.createpushreplication (syncURL);        Pushreplication.setcontinuous (TRUE);        Replication Listeners Pullreplication.addchangelistener (Pullreplicationlistener);        Pushreplication.addchangelistener (Pushreplicationlistener);        Start both replications Pullreplication.start ();    Pushreplication.start (); }//call those methods in the oncreate@override protected void OnCreate (Bundle savedinstancestate) {Super.oncrea        Te (savedinstancestate);        Setcontentview (R.layout.activity_main);            try {initdb ();        Startsync ();        } catch (Exception e) {e.printstacktrace (); }    }

The only thing left-to-do is-define some data in the database and show it to the users.

When to use Couchbase Mobile?

As always, your should find the best tool for the problem at hand. If your data is well structured and stable with no-for-modification, then-standard relational databases is the-the-t O go.

If your data is flexible and should are available anytime and anywhere, this was by far the simplest solution.

By synchronizing data from the server to the phone and working offline, Couchebase Mobile may be the best solution available today:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.