通过REST客户端 - 服务器同步 [英] Client-server synchronization over REST

查看:105
本文介绍了通过REST客户端 - 服务器同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是什么,我认为是在安卓/ iOS开发一个非常普遍的问题,但我还没有发现任何标准的解决方案呢。

This question is about what I think is a very common problem in Android / iOS development but I haven't found any "standard" solution yet.

让我们说我们有一个相当正常的REST API。服务器数据库包含(其中包括)表国家城镇以1:N的关系

Let's say we have a fairly normal REST API. The server database contains (among others) the tables countries and towns with a 1:N relationship.

客户端(手机应用程序)要保持这两个表的本地快照。所以,当它的下线,就可以做到这一点就可以正常进行了REST查询,例如:得奥城镇人口的名单> = 100

The client (mobile app) wants to maintain a local snapshot of these two tables. So that when it's offline, it can do queries that would be normally done over REST, e.g.: "get a list of Austrian towns with population >= 100"?

如何处理呢?

第一个问题:一致性。客户端应该具有两个表的快照。如果城镇表的客户端下载更新和下线,一些镇可参照的国家,这不是在国家的本地副本表。

First problem: consistency. The client should have a snapshot of the two tables. If the client downloads updates of the towns table and goes offline, some town may reference a country that's not in the local copy of the countries table.

问题二:客户端只下载新的/删除/修改的行。沟休息和使用像一些自定义的RPC调用 get_updates_since(...)

Second problem: the client should only download the new / deleted / changed rows. Ditch the REST and use some custom RPC call like get_updates_since(...)?

第三个问题:如何本地数据库更改(可能脱机使用)的客户端副本与服务器同步?自定义RPC调用?

Third problem: how should local changes to the client's copy of the database (possibly made offline) be synchronized with the server? Custom RPC calls?

推荐答案

我不认为有一个灵丹妙药,但你要寻找的模式是高速缓存。在过去的项目我已经从服务器到本地存储器(SQLite的或平面文件)复制的项目,并维持在约更新基于简单的规则/上传/吹扫的条目的元数据。这不是一个简单的问题,并最终被大量的code。

I don't think there's a silver bullet but the pattern you're looking for is caching. In past projects I have copied items from the server to local storage (SQLite or flat file), and maintained meta-data about the entries to update/upload/purge based on simple rules. It's not a simple problem and ends up being a lot of code.

一致性:在你的榜样,无论是确保您下载国表第一,或进行这两种操作原子 - 例如,使新表副本,并只替换缓存的版本,如果两个副本都成功完成(双打上存储)。

Consistency: in your example, either ensure you download the countries table first, or make the two operations atomic - e.g., make copies of the "new" tables and only replace your cached versions if both copies complete successfully (doubles up on storage).

只有下载新的/删除/修改:是的,这需要客户端/服务器集成 - 时间戳的所有记录(与格林尼治标准​​时间),要求从元数据服务器,然后步行通过当地的元数据决定做什么。 (在行提示的UUID是有益的)。

Only download new/deleted/changed: Yep, this requires client/server integration - timestamp all the records (with GMT), ask for metadata from the server, and walk through your local metadata deciding what to do. (Hint UUIDs on the rows are helpful).

同步本地更改:是的,更多的客户机/服务器集成。见上面的段落。

Synchronize local changes: Yes, more client/server integration. See above paragraph.

处理所有的异常和边缘的情况下是具有挑战性的。看看在问题的iCloud正在同步的coredata 。也许不是一个公平的比较,因为苹果正试图解决这个为一个完全分布式的数据库,但有趣的阅读不过。

Handling all the exceptions and edge cases is challenging. Take a look at the problems with iCloud sync'ing CoreData. Perhaps not a fair comparison, as Apple is trying to solve this for a totally distributed database, but interesting reading nonetheless.

这篇关于通过REST客户端 - 服务器同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆