将数据从服务器端同步到手机上, 并且需要离线工作,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提供的在线视频解析下载 ...
随机推荐
- SqLiter
1.去重 select * from daydata where wtid||rectime in (select wtid||rectime from daydata group by wtid| ...
- 静态变量static
1.在我们平时些程序中我们经常需要一些变量的他们能纪录我们所需要的值,但是平常的int类型达不到这样的目的,所以呢就有了static的基态变量,静态变量就像 B/S中的session,cookio等 ...
- C# 封装 System.Data.SQLite
参考1: 关于如何使用System.Data.SQLite的入门: http://www.dreamincode.net/forums/topic/157830-using-sqlite-with-c ...
- Java内部类总结
内部类是一种编译器现象,与虚拟机无关.编译器将会把内部类翻译成用美元符号$分隔外部类名与内部类名的常规类文件,而虚拟机对此一无所知.编译器为了引用外部类,生成了一个附加的实例域this$0 为什么要用 ...
- Lucene学习之初步了解
全文搜索 比如,我们一个文件夹中,或者一个磁盘中有很多的文件,记事本.world.Excel.pdf,我们想根据其中的关键词搜索包含的文件.例如,我们输入Lucene,所有内容含有Lucene的文件就 ...
- Android 技巧记录
1.取消EditText自动获取焦点 在项目中,一进入一个页面, EditText默认就会自动获取焦点,弹出输入法界面,很不友好.那么如何取消这个默认行为呢? 解决之道:在EditText的父级控件中 ...
- gulp配置版本号 教程之gulp-rev-append
简介: 使用gulp-rev-append给页面的引用添加版本号,清除页面引用缓存. 1.安装nodejs/全局安装gulp/项目安装gulp/创建package.json和gulpfile.js文件 ...
- 在ubuntu10.0.4下更新git
今天想到要在ubuntu10.0.4下下载android的源码学习一下.源码下载用到了git.以前安装过git以为应该没什么问题的,没想到报了 “fatal: git 1.7.2 or later r ...
- ListView学习小结
ListView小结 ListView 是Android UI中十分重要的一个组件,在数据的显示上能有着十分灵活的表现,其使用也比较简单,一般包括以下几个要点: 1. 可以通过编写ListActiv ...
- GDI+简单现实文字旋转
原文 http://www.cnblogs.com/kaixiangbb/p/3301272.html 题记 入职新公司已快有两月了,试用期已快结束,项目却迟迟还未正式启动.安排给我的多是些琐事,一直 ...