我正在使用jqueryMobile + knockout + breeze + WebAPI编写一个简单的“待办事项-你好世界”应用程序,以学习移动环境下(不可靠的互联网连接)的单页应用程序(SPA)。
为了实现离线使用,WebApp将利用:
- 应用程序缓存
- 本地存储
该应用程序应尽可能使用远程数据库来加载和保存数据,但是在离线时应能够无缝切换到本地存储,并在重新联机时同步本地/远程更改。
现在回到问题:
应用程序将使用Breeze的EntityManager来管理数据(本地缓存和远程同步)
- "remoteDb"
为了减轻不一致性/并发问题,我将使用2个本地存储键:
- “localDb”用于远程数据库的本地副本(待办事项列表)
- “localPendingChanges”用于未能提交到远程数据库的更改
因此,大致流程将是(伪代码):
LoadData
if(online)
load remoteDb
save localDb // save a local copy of the fresh loaded remotDb
if(localPendingChanges)
load localPendingChanges // we are merging in the Breeze entityManager remote data with localPendingChanges
Savedata // we are online and we have pending changes, so we should sync everything back to the remoteDb as soon as possible
if(offline)
load localDb
if(localPendingChanges)
load localPendingChanges // we are merging in the Breeze entityManager local data with localPendingChanges
SaveData
if(online)
save remoteDb
clear localPendingChanges // until we are online there is no need to keep localPendingChanges
LoadData // we are loading data from remoteDb to update our localDb to the latest version
if(offline)
save localPendingChanges // we are saving only the changes to localstorage
你对这种方法有什么看法?它混乱吗?还是可以接受的?在多用户场景中存在并发问题怎么办?