具有同步功能的 iPhone 离线应用程序
我正在考虑构建一个可以在离线状态下和在线状态下一样运行的应用程序。 由于应用程序在离线状态下无法与服务器通信,因此需要进行某种程度的同步。
在为您的 iPhone 规划离线同步操作时,有哪些值得阅读和开始思考的好工具?
与苹果已经提供的工具来帮助解决这个特定问题相比,我需要自己创建哪些工具?
I'm looking into building an application which works just as well offline as it does online. Since the application cannot communicate with the server while in offline, there is some level of synchronization which needs to take place.
What are some good tools to read about and start thinking about when planning offline operations with synchronization for your iPhone?
What tools would I have to create on my own, versus tools that apple already provides to help with this particular issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在过去的两个月左右的时间里,我一直在开发一个可以处理这种确切行为的应用程序。 它具有一小部分仅在线功能和大量离线/在线功能。
我按照此处的建议使用 sqlite 进行本地存储,并使用 sqlitepersistentobjects 库的修改版本。 sqlitepersistentobjects 的基本版本不是线程安全的,因此如果您正在使用它,请小心。 (查看objectiverecord:objectivesync作为线程安全的替代方案,但准备好深入研究代码)。 如果您愿意为 3.0 sdk 进行开发,那么核心数据是 sqlite 库的另一种可能性。
整体架构非常简单,我使用 sqlite 构建了本地存储模型,并使用 目标资源 构建了远程交互模型针对 Rails 应用程序和 REST api。 它可以使用 xml 或 json 进行数据序列化。
当本地修改对象时,更改首先保存到该对象的 sqlite 数据库记录中,然后添加到队列中,该队列也被序列化并存储在本地 sqlite 数据库中。 (然后可以随时处理队列)
如果有可用的连接,则任何排队的本地更改都会被反序列化并添加到 NSOperationQueue 中,然后由 NSOperationQueue 在后台处理它们。
为了使这一切顺利进行,我对 NSOperation 进行了子类化,以便它可以支持多种类型的远程队列操作 - 创建、更新、删除,本质上是使用目标资源来发出远程请求。
使用 NSOperationQueue 和 NSOperation 的好处是它们可以为您处理后台线程,因此我强烈建议您查看这些类的苹果文档以及苹果 线程指南。
当应用程序加载时,会在后台完成一些远程检查并进行处理以获取最新数据 - 尽管说实话我仍在稍微改变其行为方式。
这是我迄今为止必须处理的问题的快速概述......希望它能有所帮助。
I've been working on an app that handles this exact behavior for the last 2 months or so. It has a small subset of functions which are online only and a large set of functionality that is offline/online.
I'm using sqlite for local storage as suggested here with a modified version of the sqlitepersistentobjects library. The base version of sqlitepersistentobjects is not thread safe so watch out if you are using it. (check out objectiverecord in: objectivesync for a thread safe alternative but be prepared to dig into the code). If you are willing to develop for the 3.0 sdk then core data is another possibility for a sqlite library.
The overall architecture is simple enough I have modeled local storage using sqlite and remote interaction using objective resource against a rails app and REST api. It can use either xml or json for data serialization.
When an object is modified locally the change is first saved to the sqlite database record for that object and then added to a queue which is serialized and stored in the local sqlite db as well. (The queue can then be processed at any time)
If there is a connection available any queued local changes are deserialized and added to an NSOperationQueue which then processes them in the background.
In order to make this all work I've subclassed NSOperation so that it can support several types of remote queue operations - create, update, delete essentially using objective resource to make the remote requests.
The nice thing about using NSOperationQueue and NSOperation is that they handle the background threading for you so I'd highly recommend having a look at the apple docs for those classes and also at the apple threading guide.
When the application loads there is a bit of remote checking done and processed in the background to pull down the latest data - although to be honest I am still changing the way this behaves a bit.
That's a quick overview of what I've had to deal with so far...hope it helps a little.
应用程序商店中有很多应用程序都依赖于在线和离线数据,
您真正应该做的是在应用程序启动时运行后台线程(它以静默方式运行,因此您的用户永远不会看到任何延迟)。 该线程从您的服务器下载最新数据并将其推送到本地数据库(sqlite 是最佳选择)
确保您实现某种数据版本控制,以便您的应用程序仅下载自上次下载以来实际更改的数据 - 否则您会不必要下载整个数据集,该数据集可能非常大(取决于您的应用程序要求),
同时请确保在执行此操作时测试互联网连接。 如果没有可用的互联网,请确保提醒用户,
这样您就可以两全其美。 用户在远离互联网时仍然可以
在 iphone os 3.0 中使用您的应用程序及其本地 sqlite 数据苹果引入了推送服务 - 您可以简单地“推送”您的数据而不是“拉取”,但这在当前的 iPhone 中不可用操作系统 (2.xx)
there are plenty of application on the app store which rely on both online as well as offline data
what you should really be doing is on start of your app, run a background thread (which runs silently so your user never sees any delay). this thread downloads the latest data from your server and pushes it into your local database (sqlite is the best choice)
make sure you implement some kind of data versioning so that your app only downloads data which is actually changed since last download - else you would unnecessarily be downloading the entire dataset which can be quite huge (depending upon your app requirements)
also make sure to test for internet connectivity when doing this. if no internet is available, alert the user for sure
this way you get the best of both worlds. users when away from internet can still use your app with their local sqlite data
in iphone os 3.0 apple has introduced push services - where you can simply "PUSH" your data instead of doing a "PULL" however this is not available in the current iPhone OS (2.x.x)
推送在这里可能不是一个可行的选择,因为您可以推送的数据量很小,并且基本上会返回“告诉我的应用程序进行服务器调用”。 我们在 Satchel 中使用在线/离线模型。 每当我们必须与服务器通信时,我们都会捆绑该通信(一个 URL 和可能的一些 POST 数据)并将其存储到数据库中。 如果我们在线,我们会立即将其拉出并发送,当我们收到有效响应时,我们会从数据库中删除该记录。 如果我们离线,这些行就会堆积起来,下次我们在线时,它们就会被发送出去。 这不是在所有情况下都可行的模型,但可以适应大多数情况。
在 3.0 中,您可以访问 CoreData,这是一个很棒的数据管理工具。 除此之外,NSURLXXX 系列是您的朋友。
Push is probably not a viable option here, since the amount of data you can push is miniscule, and basically comes back to "tell my app to make a server call". We use an online/offline model in Satchel. Whenever we have to communicate with the server, we bundle that communication (a URL and possibly some POST data) and store it to a database. If we're online, we pull it right back out, send it, and when we get a valid response back, we remove the record from the database. If we're offline, those rows build up, and the next time we ARE online, they get sent out. This is not a workable model in all situations, but can be adapted to most.
In 3.0, you've got access to CoreData, which is a great data management tool. Other than that, the NSURLXXX family is your friend.
我会将离线时收集的所有信息存储在 SQLite 数据库中。 然后,根据用户的请求,您可以使用 HTTP 或您可以使用的自定义 TCP/IP 协议将所有存储的信息与服务器同步。
我在 Palm OS 应用程序上使用这种方法已经近 10 年了,而且它们确实非常有效。
据我所知,完成此任务所需的唯一“工具”是带有 Cocoa Touch 的普通老式 OBJECTIVE-C。 尽管您可以使用一些 TCP/IP C++ 库,但如果您决定实现自己的协议,它们将使您的生活变得更轻松。
I would store all the information I gather while offline in a SQLite database. Then, on user 's request, you can SYNC all the stored information with a server using HTTP or a custom TCP/IP protocol you can come up with.
I have been using this approach on Palm OS applications for almost 10 years now, and they do work very effectively.
As far as I know, the only "tool" you will have to accomplish this is plain old OBJECTIVE-C with Cocoa Touch. Although you could use some TCP/IP C++ libraries that will make your life easier if you decide to implement your own protocol.
想知道您是否考虑过使用同步框架来管理同步。 如果您对此感兴趣,可以查看开源项目 OpenMobster 的同步服务。 您可以执行以下同步操作:
除此之外,所有修改都会自动跟踪并与云端同步。 当网络连接断开时,您可以让应用程序离线。 它将跟踪任何更改,并在连接恢复时自动在后台将其与云同步。 它还提供像 iCloud 一样跨多个设备的同步功能
。此外,云中的修改是使用推送通知进行同步的,因此即使数据存储在本地,数据也始终是最新的。
以下是开源项目的链接:http://openmobster.googlecode.com
这里是 iPhone 的链接应用程序同步:http://code.google.com/p/openmobster/wiki/iPhoneSyncApp< /a>
Wonder if you have considered using a Sync Framework to manage the synchronization. If that interests you can take a look at the open source project, OpenMobster's Sync service. You can do the following sync operations
Besides that, all modifications are automatically tracked and synced with the Cloud. You can have your app offline when network connection is down. It will track any changes and automatically in the background synchronize it with the cloud when the connection returns. It also provides synchronization like iCloud across multiple devices
Also, modifications in the Cloud are synched using Push notifications, so the data is always current even if it is stored locally.
Here is a link to the open source project: http://openmobster.googlecode.com
Here is a link to iPhone App Sync: http://code.google.com/p/openmobster/wiki/iPhoneSyncApp