天天看點

将資料從伺服器端同步到手機上, 并且需要離線工作,Couchebase Mobile 也許是目前最好的解決方案:

将資料從伺服器端同步到手機上, 并且需要離線工作,Couchebase Mobile 也許是目前最好的解決方案:

原文位址:

<a>https://www.infinum.co/the-capsized-eight/articles/server-client-syncing-for-mobile-apps-using-couchbase-mobile</a>

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.

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

将資料從伺服器端同步到手機上, 并且需要離線工作,Couchebase Mobile 也許是目前最好的解決方案:

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.

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

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.

Here is a code example of the bare minimum needed to implement bidirectional replication from your Android application;

The only thing left to do is to define some data in the database and show it to the users.

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.