将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:
将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase 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.
将数据从服务器端同步到手机上, 并且需要离线工作,Couchebase Mobile 也许是目前最好的解决方案:的更多相关文章
- android: 将程序运行到手机上
8.3.1 将程序运行到手机上 不必我多说,首先你需要拥有一部 Android 手机.现在 Android 手机早就不是什么稀罕 物,几乎已经是人手一部了,如果你还没有话,抓紧去购买吧. 想要将程 ...
- 【NFS项目实战二】NFS共享数据的时时同步推送备份
[NFS项目实战二]NFS共享数据的时时同步推送备份 标签(空格分隔): Linux服务搭建-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品, ...
- 标 题: 有什么办法快速把pc上的网址发送到手机上
标 题: 有什么办法快速把pc上的网址发送到手机上 transfer2u, pushbullet都可以实现你说的功能,还可以把图片或者选中内容/剪贴板内容发送到手机.后者功能更强,还支持在电脑之间发 ...
- 客户端与服务器端同步Evernote
原文地址:http://www.zhihu.com/question/20238731 Evernote的同步方式是 以本地为基准同步到网络 还是 以网络为基准同步到本地 的? 若客户端从未与服务器端 ...
- MYSQL数据库主主同步实战
MYSQL支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环.当一个从服务器连接到主服务 ...
- rsync+sersync实现数据文件实时同步
一.简介 sersync是基于Inotify开发的,类似于Inotify-tools的工具: sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字: ...
- CentOS 7 Sersync+Rsync 实现数据文件实时同步
rsync+inotify-tools与rsync+sersync架构的区别? 1.rsync+inotify-tools inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪 ...
- MySQL数据实时增量同步到Kafka - Flume
转载自:https://www.cnblogs.com/yucy/p/7845105.html MySQL数据实时增量同步到Kafka - Flume 写在前面的话 需求,将MySQL里的数据实时 ...
- 如何把手机app的视频下载到手机上?网页上的视频怎么下载?
手机上小视频怎么下载?求推荐不需要安装软件的下载方法? 如何把手机app的视频下载到手机上?比如把快手上的视频下载到手机上? 如何免费下载视频? ... 答案当然是用iiiLab提供的在线视频解析下载 ...
随机推荐
- button属性值
AccessibilityObject 取得指定給控制項的 AccessibleObject. (繼承自 Control). AccessibleDefaultActionDescription 取得 ...
- 引言:Canvas绘图API快速入门
引言:Canvas绘图API快速入门 在接触HTML5的初学者包括我都在很多地方见到非常炫的一些页面,甚至好多学习HTML5的开发者都是冲着Web端的页游去的,那么HTML5那么绚丽的页面效果以及游戏 ...
- HTTP概念进阶
1.什么是回调? 在Java中,就是类A调用类B中的某个方法b,然后类B又在某个时候反过来调用类A中的某个方法a,对于A来说,这个a方法便叫做回调方法 pubilc interface CallBac ...
- Intent组件
Android页面之间的跳转可以分为几类,比如有A和B两个页面,从A页面直接跳转到B页面,这样的跳转比较简单,或者从A页面跳转到B页面但是需要传送数据.在Android中,页面的跳转离不开Intent ...
- dom 删除和清除
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Spring事务讲解示例
Spring 事务Transaction1.事务的属性1.1 事务隔离IsolationLevel1.2 事务传播PropagationBehavior1.3 事务超时Timeout1.4 只读状态R ...
- n条直线的最多交点
#include <iostream>using namespace std;int main(){int i,n;while(cin>>n){if(n==0||n==1) c ...
- Flink Program Guide (4) -- 时间戳和Watermark生成(DataStream API编程指导 -- For Java)
时间戳和Watermark生成 本文翻译自Generating Timestamp / Watermarks --------------------------------------------- ...
- php redis数据库操作类
<?php namespace iphp\db; use iphp\App; /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时, ...
- jchat:linux聊天程序1:简介
做一个linux的聊天软件,虽然没什么创意,但是它可以用来锻炼和测试我对网络编程的掌握程度,也借此机会做一些有意思的程序. 这里做的是linux下一个命令行的客户端与服务器的聊天程序,没写界面,因为对 ...