2012-07-28 60 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. 

我想这应该服务器的目的。在如何实施这个留下评论。