将数据从服务器端同步到手机上, 并且需要离线工作,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.

目录
相关文章
|
21天前
|
存储 数据可视化 C语言
【C语言】C语言 手机通讯录系统的设计 (源码+数据+论文)【独一无二】
【C语言】C语言 手机通讯录系统的设计 (源码+数据+论文)【独一无二】
|
3天前
|
SQL 数据库 数据安全/隐私保护
服务器数据恢复—raid5阵列故障因操作不当导致数据无法恢复的案例
服务器数据恢复环境: 一台服务器中有一组由4块SCSI硬盘组建的raid5磁盘阵列,划分了一个逻辑卷,操作系统为WINDOWS SERVER,作为SQL SERVER服务器使用。 服务器故障: 运行过程中该服务器raid5磁盘阵列瘫痪,管理员检查服务器发现raid5阵列中已经有3块磁盘离线。管理员选择其中2块离线硬盘进行强制上线操作,强制上线后操作系统无法启动。使用WINPE光盘启动操作系统后,可以看到数据。
|
4天前
|
JSON API 数据格式
基于服务器响应的实时天气数据进行JSON解析的详细代码及其框架
【8月更文挑战第25天】这段资料介绍了一个使用Python从服务器获取实时天气数据并解析JSON格式数据的基本框架。主要分为三个部分:一是安装必要的`requests`库以发起HTTP请求获取数据,同时利用Python内置的`json`库处理JSON数据;二是提供了具体的代码实现,包括获取天气数据的`get_weather_data`函数和解析数据的`parse_weather_data`函数;三是对代码逻辑进行了详细说明,包括如何通过API获取数据以及如何解析这些数据来获取温度和天气描述等信息。用户需要根据实际使用的天气API调整代码中的API地址、参数和字段名称。
|
2月前
|
存储 弹性计算 云计算
云服务器 ECS产品使用问题之如何在升级ECS配置时保护数据
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
2月前
|
存储 数据挖掘
服务器数据恢复—EMC存储崩溃后如何恢复存储中raid5阵列数据?
服务器存储数据恢复环境: 一台EMC存储中有一组raid5磁盘阵列,划分1个lun供小型机使用,上层采用ZFS文件系统。 服务器存储故障: 一台有一组raid5磁盘阵列的存储在运行过程中突然崩溃。管理员检查发现存储中的raid5阵列有两块硬盘离线,该阵列中的两块热备盘只有一块热备盘激活成功,raid5阵列瘫痪,存储不可用。
|
2月前
|
弹性计算 Linux 数据安全/隐私保护
云服务器 ECS产品使用问题之如何迁移游戏数据
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
2月前
|
存储 弹性计算 大数据
阿里云ECS以其强大的弹性计算与存储能力,为大数据处理提供了灵活、高效、成本优化的解决方案
阿里云ECS在大数据处理中发挥关键作用,提供多样化实例规格适应不同需求,如大数据型实例适合离线计算。ECS与OSS集成实现大规模存储,通过Auto Scaling动态调整资源,确保高效运算。案例显示,使用ECS处理TB级数据,速度提升3倍,成本降低40%,展现其在弹性、效率和成本优化方面的优势。结合阿里云生态系统,ECS助力企业数据驱动创新。
49 1
|
2月前
|
存储 弹性计算 运维
可观测性体系问题之ECS管控对日志数据的处理如何解决
可观测性体系问题之ECS管控对日志数据的处理如何解决
52 0
支付系统35-----支付成功异步通知----数据锁,微信那边是有一个服务器集群的,不单单是有一个通知发送过来,有可能有两台更多台的服务器发送过来,把锁加到我们处理通知里面,在对业务数据进行状态检查和
支付系统35-----支付成功异步通知----数据锁,微信那边是有一个服务器集群的,不单单是有一个通知发送过来,有可能有两台更多台的服务器发送过来,把锁加到我们处理通知里面,在对业务数据进行状态检查和
|
2月前
|
Web App开发
软件开发常见流程之移动端调试方法,利用Chrome(谷歌浏览器)的模拟手机调试,搭建本地Web服务器,手机和服务器在一个局域网,通过手机访问服务器,使用服务器,利用ip实现域名访问
软件开发常见流程之移动端调试方法,利用Chrome(谷歌浏览器)的模拟手机调试,搭建本地Web服务器,手机和服务器在一个局域网,通过手机访问服务器,使用服务器,利用ip实现域名访问

热门文章

最新文章

下一篇
云函数