`
zl4393753
  • 浏览: 332825 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Best architecture for an iOS application that makes many network requests?

 
阅读更多
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!
分享到:
评论

相关推荐

    Professional iOS Network Programming

    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...

    iOS Application Programming Guide

    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 ...

    Professional iOS Network Programming 2013

    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...

    Professional iOS Network Programming pdf

    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 Constrained Application Protocol

    RFC8516 "Too Many Requests" Response Code for the Constrained Application Protocol

    Wrox.Professional.iOS.Network.Programming.2012

    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...

    Python.Requests.Essentials.1784395412

    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 ...

    Python Requests Essentials.pdf 最新 原版

    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 ...

    Hacker's Guide to Scaling Python

    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 ...

    Beginning Machine Learning in iOS: CoreML Framework

    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 ...

    Apress.Beginning.Machine.Learning.in.iOS.CoreML.Framework

    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 ...

    ASP.NET Core: Cloud-ready, Enterprise Web Application Development

    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 ...

    RemObjects Data Abstract for Delphi - 9.2.101.1295 full sources

    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 ...

    Elasticsearch Best Practice Architecture

    – 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 ...

    Python Requests Essentials

    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 ...

    iOS 9 Swift Programming Cookbook.pdf

    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 ...

    ModLink2.42 full source xe10

    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....

    iOS 8 SDK Development(2nd).pdf 2015 无水印pdf

    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-crx插件

    Network Requests Monitor for DevelopersMonitor all your network requests for current tab, list them, filter them , copy the and enjoy this plugin.当前标签的网络资源请求监控,可对结果进行过滤,复制。...

Global site tag (gtag.js) - Google Analytics