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

目录
相关文章
|
1月前
|
Ubuntu Linux Android开发
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
本文介绍了如何在Android设备上安装Termux和AnLinux,并通过这些工具运行Ubuntu系统和桌面环境。
132 2
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
|
28天前
|
存储 数据挖掘
服务器数据恢复—用RAID5阵列中部分盘重建RAID5如何恢复原raid5阵列数据?
服务器数据恢复环境: 一台服务器挂接一台存储,该存储中有一组由5块硬盘组建的RAID5阵列。 服务器故障: 存储raid5阵列中有一块硬盘掉线。由于RAID5的特性,阵列并没有出现问题。工作一段时间后,服务器出现故障,用户方请人维修。维修人员在没有了解故障磁盘阵列环境的情况下,用另外4块硬盘(除去掉线的硬盘)重新创建了一组全新的RAID5阵列并完成数据同步,导致原raid5阵列数据全部丢失。
|
1月前
|
监控 网络安全 调度
Quartz.Net整合NetCore3.1,部署到IIS服务器上后台定时Job不被调度的解决方案
解决Quartz.NET在.NET Core 3.1应用中部署到IIS服务器上不被调度的问题,通常需要综合考虑应用配置、IIS设置、日志分析等多个方面。采用上述策略,结合细致的测试和监控,可以有效地提高定时任务的稳定性和可靠性。在实施任何更改后,务必进行充分的测试,以验证问题是否得到解决,并监控生产环境的表现,确保长期稳定性。
60 1
|
2月前
|
存储 弹性计算 缓存
阿里云服务器ECS通用型实例规格族特点、适用场景、指标数据解析
阿里云服务器ECS提供了多种通用型实例规格族,每种规格族都针对不同的计算需求、存储性能、网络吞吐量和安全特性进行了优化。以下是对存储增强通用型实例规格族g8ise、通用型实例规格族g8a、通用型实例规格族g8y、存储增强通用型实例规格族g7se、通用型实例规格族g7等所有通用型实例规格族的详细解析,包括它们的核心特点、适用场景、实例规格及具体指标数据,以供参考。
阿里云服务器ECS通用型实例规格族特点、适用场景、指标数据解析
|
1月前
|
Python
Flask学习笔记(三):基于Flask框架上传特征值(相关数据)到服务器端并保存为txt文件
这篇博客文章是关于如何使用Flask框架上传特征值数据到服务器端,并将其保存为txt文件的教程。
31 0
Flask学习笔记(三):基于Flask框架上传特征值(相关数据)到服务器端并保存为txt文件
|
2月前
|
存储 弹性计算 安全
阿里云服务器ECS计算型实例规格族特点、适用场景、指标数据参考
阿里云服务器ECS提供了丰富的计算型实例规格族,专为满足不同场景下的高性能计算需求而设计。包括计算型实例规格族c8y、计算型实例规格族c7、计算型实例规格族c8i等热门计算型实例规格,以及网络增强型的c7nex、密集计算型的ic5等其他计算型实例规格,每一种规格族都经过精心优化,确保在计算性能、存储效率、网络吞吐和安全特性等方面达到最佳平衡。本文将详细解析阿里云服务器ECS中的多个计算型实例规格族,包括它们的核心特点、适用场景、实例规格及具体指标数据,为用户在云计算资源选型时提供全面参考。
阿里云服务器ECS计算型实例规格族特点、适用场景、指标数据参考
|
1月前
|
SQL 分布式计算 关系型数据库
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
53 3
|
1月前
|
弹性计算 Linux 数据库
阿里云国际版如何迁移Linux云服务器系统盘中的数据
阿里云国际版如何迁移Linux云服务器系统盘中的数据
|
2月前
|
运维 数据挖掘 开发工具
服务器数据恢复—硬盘离线导致raid5阵列热备盘上线失败的数据恢复案例
服务器磁盘阵列数据恢复环境: 服务器中有两组分别由4块SAS硬盘组建的raid5磁盘阵列,两组raid5阵列划分LUN,组成LVM结构,格式化为EXT3文件系统。 服务器磁盘阵列故障: 服务器中一组raid5阵列中有一块硬盘离线,热备盘自动上线替换离线硬盘。热备盘上线同步数据过程中又有一块硬盘离线,热备盘同步失败,该组raid5阵列崩溃,LVM结构变得不完整,文件系统无法使用。 硬件工程师对两块离线硬盘进行硬件故障检测,发现先离线硬盘无法识别,初步判断该硬盘存在硬件故障,需要进行开盘修复。后离线硬盘可以正常识别。
服务器数据恢复—硬盘离线导致raid5阵列热备盘上线失败的数据恢复案例
|
1月前
|
编解码 弹性计算 运维
AWS无服务器直播解决方案
AWS无服务器直播解决方案
下一篇
无影云桌面