`
zl4393753
  • 浏览: 340279 次
  • 性别: 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!
分享到:
评论

相关推荐

    ios application programming guide

    The tag "programming guide" suggests that the document provides detailed instructions and best practices for iOS app development. #### Partial Content The partial content provides legal information...

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

    RFC8516文档介绍了一种针对Constrained Application Protocol(CoAP)的新响应码“Too Many Requests”(RFC8516)。CoAP是专门设计用于在受限环境中运行的互联网应用协议,尤其适用于资源受限的嵌入式设备。该协议...

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

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

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

    Professional iOS Network Programming pdf

    《Professional iOS Network Programming》这本书为读者提供了一系列的指导和技巧,涵盖了iOS应用与企业网络资源、其他平台和设备之间的网络通信的方法和技巧。本书不仅仅是关于iOS与企业网络的结合,更注重于iOS与...

    Packt.Mastering.Swift.3.Linux.2017

    Swift 3 is a significant update that brings numerous improvements and optimizations, making it an ideal choice for developing applications not only for iOS, macOS, watchOS, and tvOS but also for the ...

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

    PhoneGap 3.x Mobile Application Development Hotshot.2014.pdf

    **PhoneGap 3.x Mobile Application Development Hotshot.2014.pdf** is a comprehensive guide designed to help developers create engaging and practical mobile applications for iOS and Android devices ...

    Apress.Pro.Core.Data.for.iOS.2nd.Ed

    《Apress.Pro.Core.Data.for.iOS.2nd.Ed》是一本专为iOS开发者深入解析Core Data技术的书籍。Core Data是苹果公司提供的一种数据管理框架,它简化了在iOS和macOS应用中处理对象关系映射的过程。这本书的第二版详细...

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

    requests-2.23.0.tar.gz

    8. **错误处理**:当请求失败时,Requests会抛出相应的异常,如`ConnectionError`、`Timeout`和`TooManyRedirects`,便于开发者捕获并处理错误。 9. **证书验证**:支持SSL证书验证,可以自定义证书路径或者关闭...

    python-requests离线包

    本离线包“python-requests”正是为了解决在无网络环境下也能使用这个库的需求。 首先,我们来详细解释一下压缩包中的各个文件: 1. `python-3.7.4-amd64.exe`: 这是Python 3.7.4的安装程序,适用于AMD64架构的...

Global site tag (gtag.js) - Google Analytics