REST API 和 iPhone 应用程序架构和代码结构

发布于 2024-11-27 15:53:19 字数 505 浏览 0 评论 0原文

背景:
我正处于 iPhone 应用程序和 REST Web 服务产品的早期阶段。基本上,我在服务器上有一个数据库,并编写了一些 REST API 来访问该数据库。我有一个配套的 iPhone 应用程序将使用这些 REST API。

问题:
放置访问 REST API 的代码的最佳方法是什么?我应该创建一个单独的 NSObject 子类并将接口和实现细节放在那里吗?我希望减少整个应用程序中与 REST API 交互的各种 ViewController 中的代码重复量。

例如:
我的应用程序将启动并进行 REST API 调用,通过比较从 API 与本地返回的值来确定设备上本地信息的当前状态。然后我会从 API 发起更新请求以刷新本地数据存储。

现在,如果我在启动 ViewController 中拥有所有这些逻辑,我将使用仅具有委托的 NSURLConnection 进行多个调用。我不知道如何在同一个 ViewController 中进行这些单独的调用。

解决这个问题有哪些方法?

Background:
I am in the early stages of an an iPhone App and REST WebService product. Basically, I have a database on the a server and have written some REST APIs to access this database. I have a companion iPhone app that will consume these REST APIs.

Questions:
What is the best approach to place the code for accessing the REST APIs? Should I create a separate subclass of NSObject and place the interface and implementation details there? I would like to reduce the amount of code duplication throughout the application in the various ViewControllers that would interact with the REST API.

For Example:
My app would start up and make a REST API call to determine the current state of local information on the device by comparing the value returned from the API vs local. Then I would initiate an update request from the API to refresh the local datastore.

Now if I have all of this logic in the start-up ViewController I have multiple calls using NSURLConnection which only has on delegate. I don't know how to make these separate calls in the same ViewController.

What are some approaches to solving this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-12-04 15:53:19

我通常创建使用 ASIHTTPRequest(使用组合,而不是子类化)的自定义对象,可以使用任何所需的参数进行初始化,并使用它来管理对 RESTful 服务的请求。自定义对象将提供用于处理成功、失败和其他自定义通知的委托协议,以便我可以在异步执行请求时使用此反馈来更新应用程序 UI。

如果您需要解析从请求返回的大量数据,请确保启动一个单独的线程来执行此操作,而不是在 HTTP 请求成功回调中执行此操作,否则这将使网络活动微调器的活动时间比实际应有的时间长是。

如果您需要管理同一类型的多个请求,您可以在自定义请求中添加“tag”属性,这样当您的成功/失败委托响应被调用时,您可以轻松识别它属于哪个请求,而无需保留原始请求的实例变量。

I generally create custom objects which use ASIHTTPRequest (using composition, not subclassing) which can be initialised with any required parameters, and use that to manage requests to the RESTful service. The custom object will provide a delegate protocol for handling success, failure and other custom notifications so that I can use this feedback to update the application UI while the request is performed asynchronously.

If you need to parse large amount of data returned from a request, make sure you launch a separate thread to do this, rather than doing it in your HTTP request success callback, otherwise this will keep the network activity spinner active longer than it actually should be.

If you need to manage multiple requests of the same type, you could add a "tag" property to your custom request, so that when your success/failure delegate response is called, you can easily identify which request it belongs to without having to keep an instance variable to the original request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文