最佳架构的iOS应用程序,使许多网络请求? [英] Best architecture for an iOS application that makes many network requests?

查看:222
本文介绍了最佳架构的iOS应用程序,使许多网络请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新思考我正在开发的大型应用程序的请求体系结构。我目前使用ASIHTTPRequest实际提出请求,但由于我需要许多不同类型的请求作为在不同的视图控制器中采取的许多不同的操作的结果,我试图找出最好的系统组织这些请求。 p>

我目前正在构建由应用程序委托保留的单例请求者,并且坐在监听NSNotifications,以表示需要进行请求;他们做出请求,监听响应,并发送一个新的NSNotification与响应数据。这解决了我的大多数问题,但是没有优雅地处理失败的请求或同时请求同一单独请求者。



任何人都有成功设计一个清晰的OO架构在iOS应用程序中做出许多不同类型的请求?

解决方案

在尝试了几种方法之后,很好的结果,很容易记录,理解,维护和扩展:




  • 我有一个对象负责网络连接,让我们称之为网络管理器。通常,此对象是单例(使用 Matt Gallagher的Cocoa单例宏创建)。

  • 由于您使用ASIHTTPRequest(我一直做,精彩的API)我在我的网络管理器中添加一个ASINetworkQueue ivar。

  • 我为应用程序需要的每种网络请求(通常是为每个后端REST交互或SOAP端点)创建ASIHTTPRequest的子类。这有另一个好处(详情见下文)。

  • 每当我的一个控制器需要一些数据(刷新,viewDidAppear等),网络管理器创建一个所需的ASIHTTPRequest子类的实例,然后将其添加到队列中。

  • ASINetworkQueue负责处理带宽问题(取决于您是在3G,EDGE还是GPRS或Wifi,您有更多的带宽,处理更多请求等)。这是由队列完成的,这是很酷的(至少,这是我明白这个队列的事情之一,我希望我不会误认为)。

  • 每当一个请求完成

  • 网络管理员不知道如何处理每个请求的结果;因此,它只是在请求上调用方法!记住,请求是ASIHTTPRequest的子类,所以你可以放置管理请求结果的代码(通常,将JSON或XML反序列化为真实对象,触发其他网络连接,更新Core Data存储等)。将代码放入每个单独的请求子类中,使用具有通用名称的多态方法跨请求类,使得调试和管理IMHO非常容易。

  • 最后,我通知上面的控制器有趣的事件使用通知;使用代理协议不是一个好主意,因为在您的应用程序中,通常有许多控制器与您的网络管理器通信,然后通知更灵活(您可以有几个控制器响应相同的通知等)。



无论如何,这是我一直在做的一段时间,坦率地说,它的工作相当不错。我可以水平扩展系统,添加更多的ASIHTTPRequest子类,因为我需要他们,并且网络管理器的核心保持不变。



希望它有帮助!


I'm in the process of rethinking my approach to the request architecture of a large app I'm developing. I'm currently using ASIHTTPRequest to actually make requests, but since I need many different types of requests as a result of many different actions taken in different view controllers, I'm trying to work out the best system of organizing these requests.

I'm currently building singleton "requesters" that are retained by the app delegate and sit around listening for NSNotifications that signal a request needs to be made; they make the request, listen for the response, and send out a new NSNotification with the response data. This solves most of my problems, but doesn't elegantly handle failed requests or simultaneous requests to the same singleton requester.

Anyone have any success devising a clear, OO architecture for making many different types of requests in an iOS app?

解决方案

After having tried several approaches, this is one architecture that is giving me excellent results, is easy to document, understand, maintain and extend:

  • I have a single object taking care of network connectivity, let's call it a "network manager". Typically this object is a singleton (created using Matt Gallagher's Cocoa singleton macro).
  • Since you use ASIHTTPRequest (which I always do, wonderful API) I add an ASINetworkQueue ivar inside my network manager. I make the network manager the delegate of that queue.
  • I create subclasses of ASIHTTPRequest for each kind of network request that my app requires (typically, for each backend REST interaction or SOAP endpoint). This has another benefit (see below for details :)
  • Every time one of my controllers requires some data (refresh, viewDidAppear, etc), the network manager creates an instance of the required ASIHTTPRequest subclass, and then adds it to the queue.
  • The ASINetworkQueue takes care of bandwidth issues (depending on whether you are on 3G, EDGE or GPRS or Wifi, you have more bandwidth, and you can process more requests, etc). This is done by the queue, which is cool (at least, that's one of the things I understand this queue does, I hope I'm not mistaken :).
  • Whenever a request finishes or fails, the network manager is called (remember, the network manager is the queue's delegate).
  • The network manager doesn't know squat about what to do with the result of each request; hence, it just calls a method on the request! Remember, requests are subclasses of ASIHTTPRequest, so you can just put the code that manages the result of the request (typically, deserialization of JSON or XML into real objects, triggering other network connections, updating Core Data stores, etc). Putting the code into each separate request subclass, using a polymorphic method with a common name accross request classes, makes it very easy to debug and manage IMHO.
  • Finally, I notify the controllers above about interesting events using notifications; using a delegate protocol is not a good idea, because in your app you typically have many controllers talking to your network manager, and then notifications are more flexible (you can have several controllers responding to the same notification, etc).

Anyway, this is how I've been doing it for a while, and frankly it works pretty well. I can extend the system horizontally, adding more ASIHTTPRequest subclasses as I need them, and the core of the network manager stays intact.

Hope it helps!

这篇关于最佳架构的iOS应用程序,使许多网络请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆