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

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

  1. android: 将程序运行到手机上

    8.3.1   将程序运行到手机上 不必我多说,首先你需要拥有一部 Android 手机.现在 Android 手机早就不是什么稀罕 物,几乎已经是人手一部了,如果你还没有话,抓紧去购买吧. 想要将程 ...

  2. 【NFS项目实战二】NFS共享数据的时时同步推送备份

    [NFS项目实战二]NFS共享数据的时时同步推送备份 标签(空格分隔): Linux服务搭建-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品, ...

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

    标  题: 有什么办法快速把pc上的网址发送到手机上 transfer2u, pushbullet都可以实现你说的功能,还可以把图片或者选中内容/剪贴板内容发送到手机.后者功能更强,还支持在电脑之间发 ...

  4. 客户端与服务器端同步Evernote

    原文地址:http://www.zhihu.com/question/20238731 Evernote的同步方式是 以本地为基准同步到网络 还是 以网络为基准同步到本地 的? 若客户端从未与服务器端 ...

  5. MYSQL数据库主主同步实战

    MYSQL支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环.当一个从服务器连接到主服务 ...

  6. rsync+sersync实现数据文件实时同步

    一.简介 sersync是基于Inotify开发的,类似于Inotify-tools的工具: sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字: ...

  7. CentOS 7 Sersync+Rsync 实现数据文件实时同步

    rsync+inotify-tools与rsync+sersync架构的区别? 1.rsync+inotify-tools inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪 ...

  8. MySQL数据实时增量同步到Kafka - Flume

    转载自:https://www.cnblogs.com/yucy/p/7845105.html MySQL数据实时增量同步到Kafka - Flume   写在前面的话 需求,将MySQL里的数据实时 ...

  9. 如何把手机app的视频下载到手机上?网页上的视频怎么下载?

    手机上小视频怎么下载?求推荐不需要安装软件的下载方法? 如何把手机app的视频下载到手机上?比如把快手上的视频下载到手机上? 如何免费下载视频? ... 答案当然是用iiiLab提供的在线视频解析下载 ...

随机推荐

  1. jquery选择器从认识到使用初级篇

    1.   .class 选择器 ---一种通过元素类别属性查找元素 调用格式: $(".class") ----其中参数表示元素的css类别名称(类选择器)<input cl ...

  2. 下拉条的连动-ajax方式

    客户端触发: <select id="category1" onchange="changecategory()">    <option v ...

  3. CSS中字体尺寸总结

    下面是我总结的css中关于字体尺寸的知识,欢迎高手拍砖! 前端开发过程中,我们经常会遇到设置某个div固定显示几行文本:这时我们需要精确计算每个字号字体的宽度和高度. 下面是w3school中描述的尺 ...

  4. setTimeout的若干坑

    第一坑:作用域 首先,有一个关于this的面试题,是这样的: var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: ...

  5. Android 关于操作UI线程

    在非UI线程里访问 Android UI toolkit—这个在一个worker线程修改了 View .这会导致不可预期的结果,而且还难以调试. 为了修复这个问题,Android提供了几个方法从非UI ...

  6. android 连接网络的简单实例

    1.android有两种连接网络的类HttpURLConnect和HttpClient,但是HttpClient已逐渐被HttpURLConnect类代替所以就不提及. 2.实例 String add ...

  7. Scrapy的架构初探

    Scrapy,Python开发的一个web抓取框架. 1,引言 Python即时网络爬虫启动的目标是一起把互联网变成大数据库.单纯的开放源代码并不是开源的全部,开源的核心是“开放的思想”,聚合最好的想 ...

  8. MySQL----基本数据类型

    1.数值型: tinyint:1字节 smallint:2字节 mediumint:3字节 int:4字节 bigint:8字节 float:4字节 double:8字节 decimal(m,c):m ...

  9. ThinkPHP框架下基于RBAC的权限控制模式详解

    这几天因为要做一个项目,需要可以对Web应用中通用功能进行封装,其中一个很重要的涉及到了对用户.角色和权限部分的灵活管理.所以基于TP框架自己封装了一个对操作权限和菜单权限进行灵活配置的可控制模式. ...

  10. Google的兼容包问题【转】

    转自:http://blog.sina.com.cn/s/blog_3e28c8a50101g14g.html 项目之前好好的,今天开Eclipse,,出错. 错误Error retrieving p ...