将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:-阿里云开发者社区

开发者社区> wei-spring> 正文

将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:

简介: 将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案: 原文地址: https://www.infinum.co/the-capsized-eight/articles/server-client-syncing-for-mobile-a...
+关注继续查看

将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:

原文地址:

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 that synchronizes data from server to smartphone and needs to work offline, this is the article for you.

Every once in a while, you end up working on a project that throws you out of your comfort zone and requires some heavy duty learning.

For me, it was a seemingly simple project that required things like data replication and high availability, things I vaguely remember from my college days. After some exploring, I came across Couchbase Mobile, a framework that offers support for mobile app development and covers all of the requirements.

Couchbase Mobile has two major parts:

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

Couchbase Mobile server-client syncing

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 with. This allows for great flexibility and little maintenance.

VIEWS

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

DISTRIBUTION

Couchbase's distribution system is incredibly complex and very powerful. It has 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 to take care of 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 of the current network status.

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

How do I implement it?

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

Here is 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 database manager with default options
        Manager manager = new Manager(new AndroidContext(MainActivity.this), Manager.DEFAULT_OPTIONS);

        // get or create the database with the provided name
        database = manager.getDatabase("demodb");

        // add a change listener
        database.addChangeListener(databaseListener);
    }

//start bi-directional syncing
protected 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.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            initDB();
            startSync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

When to use Couchbase Mobile?

As always, you should find the best tool for the problem at hand. If your data is well structured and stable with no room for modification, then standard relational databases are the way to go.

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
通过端到端的数据侦测提升QoS
通过收集网络传输的数据,并采用更接近主观主观评价的客观评价方式,可以有效反映QoS的水平,从而帮助运营方改进服务。
84 0
数据同步框架MS Sync Framework - IDE快速开发支持Local Database Cache
数据同步框架MS Sync Framework [术语、例子、参考资料、Tips] http://www.cnblogs.com/2018/archive/2011/02/22/1961654.html  数据同步框架MS Sync Framework-不同场景使用例子和简要分析 http://www.cnblogs.com/2018/archive/2011/02/23/1961657.html 以上两篇文章对框架的基础有了描述,通过IDE的提供Local Database Cache可以快速的实现一个常用的同步应用。
769 0
将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:
将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案: 原文地址: https://www.infinum.co/the-capsized-eight/articles/server-client-syncing-for-mobile-a...
1368 0
阿里云服务器小白新手教程(从选配到网站上线全过程)
阿里云服务器新手教程从云服务器选配购买、云服务器配置到网站上线全过程,阿里云服务器如何购买?购买后如何使用?云集市场抽时间写了一篇详细的阿里云服务器新手教程,从云服务器选配到网站上线全过程:
3000 0
logstash-output-mongodb实现Mysql到Mongodb数据同步
本文主要讲解如何通过logstash-output-mongodb插件实现Mysql与Mongodb数据的同步。源数据存储在Mysql,目标数据库为非关系型数据库Mongodb。
13 0
fbh
js判断是否是pc端打开还是手机端打开
根据浏览器头判断是使用神马设备打开的网页 function is_mobile() { var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.
2701 0
1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结
 1记忆卡片游戏代码 CardItem.h #pragmaonce #ifndef__CardItem_H__ #define__CardItem_H__   #include"cocos2d.h" USING_NS_CC;   classCardItem :publicCCSprite {
1439 0
+关注
wei-spring
Android 开发,Java 开发,Python开发
366
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载