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!
分享到:
相关推荐
This Wrox guide provides everything you need to know to write iOS apps that integrate with enterprise network resources, providing options for networking iOS devices to enterprise systems and to each...
Understanding an Application’s States and Transitions 29 Multitasking 36 Checklist for Supporting Multitasking 37 Being a Responsible, Multitasking-Aware Application 37 3 2011-02-24 | © 2011 Apple ...
This Wrox guide provides everything you need to know to write iOS apps that integrate with enterprise network resources, providing options for networking iOS devices to enterprise systems and to each...
This Wrox guide provides everything you need to know to write iOS apps that integrate with enterprise network resources, providing options for networking iOS devices to enterprise systems and to each...
RFC8516 "Too Many Requests" Response Code for the Constrained Application Protocol
This Wrox guide provides everything you need to know to write iOS apps that integrate with enterprise network resources, providing options for networking iOS devices to enterprise systems and to each...
A fast-paced guide that demonstrates the use of Python Requests with the help of examples Learn web scraping with Beautiful Soup and Python Requests libraries Interact with social networking sites ...
A fast-paced guide that demonstrates the use of Python Requests with the help of examples Learn web scraping with Beautiful Soup and Python Requests libraries Interact with social networking sites ...
But how do you make those applications scale for thousands of users and requests? It takes years of practice, research, trial and errors to build experience and knowledge along the way. Simple ...
Many applications on iPhone use machine learning: Siri to serve voice-based requests, the Photos app for facial recognition, and Facebook to suggest which people that might be in a photo. You'll ...
Many applications on iPhone use machine learning: Siri to serve voice-based requests, the Photos app for facial recognition, and Facebook to suggest which people that might be in a photo. You'll ...
Improve performance at the network level and I/O level, and how to optimize the application as a whole Monitor performance as part of Continuous Integration and regression testing In Detail ASP.NET ...
In your client application, Data Abstract provides a simple yet flexible API that makes it easy to request data, modify it, and send it back to the server – right away or later on. All without ...
– Route requests, handle search reduce phase, distribute bulk indexing – All nodes function as coordinating nodes • Alerting Nodes – Run alerting jobs • Machine Learning Nodes – Run machine ...
the Python Requests library is one of the world’s best clients, with the highest number of downloads. It allows hassle-free interactions with web applications using simple procedures. You will be ...
Previously, almost all requests had to go to the companion iOS device from the watch and then served by the iOS app. With watchOS 2, the watch takes a lot more responsibilities and can update its ...
listens for incoming requests from a remote Modbus client and performs an appropriate action. For details on installing and using ModLink, please refer to the accompanying help file - Help\ModLink....
You'll send network requests and handle the responses, build from one screen to many, adapt from the close confines of the iPhone screen to the wide expanse of the iPad, and accommodate the big ...
Network Requests Monitor for DevelopersMonitor all your network requests for current tab, list them, filter them , copy the and enjoy this plugin.当前标签的网络资源请求监控,可对结果进行过滤,复制。...