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

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.

时间: 2024-08-05 06:56:11

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

[转载]不同服务器数据库之间的数据操作--复制同步(整理版)

备份还原: A服务器为主服务器,客户端更新都更新在A服务器上: B服务器为次服务器,B定时与A保持一致,结果B机时A的一个拷贝/备份,只是时间上有一个差. 实现方法:A定时备份;B定时以A的备份进行还原 具体方法如下:(MS-SQL中) 打开A服务器企业管理器,找到管理-SQL Server代理-作业,创建一个新作业,名字可以起备份,作业中创建一个步骤: SQL code BACKUP DATABASE [TEST] TO DISK = 'F:/SQL_SERVER/TEST/DB.BACKUP

标 题: 有什么办法快速把pc上的网址发送到手机上

标  题: 有什么办法快速把pc上的网址发送到手机上 transfer2u, pushbullet都可以实现你说的功能,还可以把图片或者选中内容/剪贴板内容发送到手机.后者功能更强,还支持在电脑之间发,但是Mac下Safari没有支持.前者应用范围更广.[ 在 gaofei 的大作中提到: ] 手机扣扣可以同步共享啊  笨-- 你竟然不知道手机和电脑同时登录QQ可以互发或者单一登录也可以?-- 比较了一下各个方案,还是微信网页版最省事  放浏览器收藏起来,用的时候直接发就行[ 在 gaofei

android开发的应用程序,用模拟器可以访问,但是安装到手机上却不行

============问题描述============ 开发一个与数据库返回数据的程序,服务端是用servlet写的,用模拟器测试时,是能够访问的,且能返回数据,但是把程序安装到手机上,却不能访问服务端了,请问是为什么啊 ============解决方案1============ 肯定的啊,你手机的ip和你模拟器的不一样!你模拟器用的是和电脑默认的一个地址,你修改下手机的ip,改成你访问路径的那个地址试试. ============解决方案2============ 服务端是你本机吗? 如果是

android: 将程序运行到手机上

8.3.1   将程序运行到手机上 不必我多说,首先你需要拥有一部 Android 手机.现在 Android 手机早就不是什么稀罕 物,几乎已经是人手一部了,如果你还没有话,抓紧去购买吧. 想要将程序运行到手机上,我们需要先通过数据线把手机连接到电脑上.然后进入到设 置→开发者选项界面,并在这个界面中勾选中 USB 调试选项,如图 8.9 所示.注意从 Android 4.2 版本开始,系统默认是把开发者选项隐藏掉的,你需要先进入到关于手机界面,然后对 着最下面的版本号那一栏连击四次,就会让开

移动oa办公自动化软件怎样把办公室搬到手机上呢?

手机,已经成为人们生活和工作中都离不开的工具,而且在工作中不再只充当联络的角色,移动oa办公自动化软件的出现让人们顺利的把工作和生活平衡好.更重要的是,移动oa办公自动化软件让人们可以随时随地进行办公,等于把整个办公室都搬到手机上,甚至放在口袋里,企业开始全面实现信息化建设. 在工作上,人们可以通过移动oa办公自动化软件随时随地进行办公,即使是在出差的过程中,都可以通过随身携带的智能手机来处理一切紧急的事务,让人们无论去到哪里都不会耽误工作的进度,更不会对企业造成任何的损失.同时人们可以自行选择

数据的实时同步

一.两主机间数据的实时同步 1.同步原理 利用监控服务(inotify),监控同步数据服务器目录中信息的变化发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 2.实现方式 inotify+rsync方式实现数据同步sersync 在inotify 软件基础上进行开发的,功能更加强大 3.inotify 异步的文件系统时间监控机制,利用事件驱动机制,而无须通过诸如cron等的轮询机制来获取事件,linux内核从2.6.13起支持 inotify,通过inotify可以监控文件系统中添

Android开发把项目打包成apk,安卓到手机上,或者提交到app应用商店

#1.用Eclipse的话导出app其实还是很容易的.大家看我的步骤.有图有真相哦 选择一个项目 创建一个新的,位置随便,下面是密码 这里都是一些名字,地区,组织,国家.看你们自己的了 选择你要存放apk的位置 打完收工 Android开发把项目打包成apk,安卓到手机上,或者提交到app应用商店,布布扣,bubuko.com

TCP协议客户端键盘录入数据,服务器端写到文本文件

package cn.idcast9; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; // 客户端键盘录入数据,服务器端写到文本文件 public class ClientDemo

MongoDB 数据迁移和同步

MongoDB 数据迁移和同步 MongoDB的数据同步 复制 mongodb的复制至少需要两个实例.其中一个是主节点master,负责处理客户端请求,其余的都是slave,负责从master上复制数据. master写处理:master负责接收写请求,具体的流程为: 如果开启journal功能,则先将写请求记录到journal中,然后批量执行,同时将操作记录到oplog中: 如果未开启journal功能,则对每个写请求进行单独操作,然后写入oplog. 注:oplog是幂等的,当有累加操作in