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

    简介: Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate可以应用在任何使 ...

  2. ios9配置info.plist中关于安全访问问题

    打开info.plist文件 - >添加App Transport Security Settings 字典类型,在App Transport Security Settings下增加Allow ...

  3. Visual C++ 64bit应用程序项目设置

    Visual Studio 2005 This topic describes how to set up C++ applications to target 64-bit platforms us ...

  4. PHP的五种常见设计模式

    工厂模式 最初在设计模式 一书中,许多设计模式都鼓励使用松散耦合.要理解这个概念,让我们最好谈一下许多开发人员从事大型系统的艰苦历程.在更改一个代码片段时,就会发生问题,系统其他部分 —— 您曾认为完 ...

  5. linux安装php5.3

    安装php的依赖包 [root@localhost admin]# unzip libxml2-2.7.8.tar.zip [root@localhost admin]#tar zvxf libxml ...

  6. 浏览器的重绘repaints与重排reflows深入分析

    重绘是一个元素外观的改变所触发的浏览器行为,浏览器会根据元素的新属性重新绘制,使元素呈现新的外观,接下来将详细介绍,需要了解的朋友可以参考下: 在项目的交互或视觉评审中,前端同学常常会对一些交互效果质 ...

  7. NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证

    JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器.每一种解析器都是一个运行环境,不但允许J ...

  8. 关于const限定符

    1.  指向非常量数据的非常量指针 具有最高的数据访问极限 .不包含const关键字. 2. 指向常量数据的非常量指针 指针可以被改写,使其指向相应类型的任何数据项, 但是它所指向的数据项的值是不能被 ...

  9. windows后台服务程序编写

    Windows后台服务程序编写 1. 为什么要编写后台服务程序 工作中有一个程序需要写成后台服务的形式,摸索了一下,跟大家分享. 在windows操作系统中后台进程被称为 service. 服务是一种 ...

  10. 百用随身系统 Veket Linux

    Veket Linux 是一个随身的可装在U盘的Linux操作系统. 特点:1,随身系统,装在U盘可走遍天下,它几乎支持“所有”的电脑,就我所接触得到的电脑它都支持并成功驱动,就这十多年的安装的电脑都 ...