2012-07-28 59 views
1

比方說,我用某種集中式數據庫構建了一個帶有iphone應用程序和Web應用程序的待辦應用程序。如果我在沒有互聯網連接的情況下在iPhone上添加待辦事項,我顯然無法在Web應用程序中看到它們。由於離線時不能進行API請求,因此數據不在數據庫中。如何構建在離線輸入數據時同步的應用程序?

爲了防止在離線時添加的任務丟失,您是否將它們存儲在本地,然後在建立互聯網連接時,將API調用創建這些任務並將它們添加到數據庫?

這究竟是如何在iOS中完成的?我只是在本地創建對象嗎?你如何做到這一點,以便它知道具體任務不在數據庫中,並且應該使API調用來實現它們?

回答

1

您需要將所有數據存儲在數據庫本地,然後檢查互聯網連接是否可用,如果互聯網可用,請通過進行API調用將數據發送到Web,然後從本地數據庫中刪除數據從服務器接收響應。 這樣從服務器獲取響應後,刪除你的本地數據庫的每一行。

此實現,你可以有:

- create a database on mobile 
- Register User on Server, After registering client should get a response from server and a timestamp. 
- Save timestamp and register a Pending Intent for 12/24 hrs to Start a Background Service that would Sync the Data to Server. 
- In case of no availability of Internet while Service wants to Sync data, we should have a Broadcast Receiver that check for internet connectivity, and as soon as Internet is available it would silently Start Service (Sync Service) in background and send data to Server. 
- Server would again send a response with timestamp, on receiving timestamp we delete our local Database, and repeat step 3. This cycle will keep on repeating. 

我想這應該服務器的目的。在如何實施這個留下評論。