如何构建一个在离线输入数据时同步的应用程序?

时间:2012-07-28 18:55:13

标签: objective-c ios database

假设我使用某种集中式数据库构建了一个带有iPhone应用程序和Web应用程序的待办事项应用程序。如果我在没有互联网连接的情况下在我的iphone上添加待办事项,我显然无法在网络应用上看到它们。由于离线时无法提出API请求,因此数据不在数据库中。

为了防止在离线时添加的任务丢失,您是将它们存储在本地,然后在建立Internet连接时,然后进行API调用以创建这些任务并将它们添加到数据库中?

如何在iOS中完成这项工作?我只是在本地构建对象吗?你如何才能让它知道特定的任务不在数据库中,并且应该进行API调用来制作它们?

1 个答案:

答案 0 :(得分: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.

我认为这应该服务于目的。如果要实现这一点,请发表评论。