NSURLRequest 和目标操作
作为一名 iOS 开发新手,我试图了解一些与 iOS 中回调机制相关的概念。
我的模型通过 NSURLRequest 向后端 REST 服务发出 HTTP 请求。该模型有几个与服务中的方法相对应的方法。 NSURLRequest 基于委托模式,这意味着我会收到所有服务调用的公共回调。然后,我的模型必须找出回调与哪个服务调用相关,以便我可以向控制器发送适当的更新事件。这很尴尬,因为我必须在模型中维护一些状态来记住我上次进行的调用(这在并发情况下非常不切实际),或者解释 HTTP 响应中的有效负载。
我希望 NSURLRequest 支持目标操作模式,以便每个请求都可以决定使用哪种回调方法。这可能吗?我在这里错过了什么吗?
如果框架中没有目标操作,解决此问题的最佳实践是什么?
As a novice iOS developer, I am trying to understand some concepts related to the callback mechanisms in iOS.
My model makes HTTP requests through NSURLRequest to a backend rest service. The model has several methods which corresponds to the methods in the service. NSURLRequest is based on the delegate pattern, which means that I receive a common callback for all of the service calls. Then, my model has to find out which service call the callback is related to, so that I can send an appropriate update event to the controller. This is awkward since I have to maintain som state in the model to remember which call I made the last time (which is very impractical in the case of concurrency), or interpret the payload in the HTTP response.
I would wish that NSURLRequest would support the target-action pattern, so that each of the requests could decide which callback method to use. Is that possible? Am I missing something here?
If target-action is not available in the framework, what are the best practices to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现这一点的方法是将 NSURLRequest 与 NSURLConnection 结合使用。如果您查看 文档对于 NSURLConnection,他们会告诉你需要实现 NSURLConnectionDelegate 协议中的回调方法,并会给你详细信息。
该页面还指向几个示例以及示例代码。
您还可以查看 URL 加载系统编程指南(位于developer.apple.com),它将为您提供有关如何使用这些类的更多信息。
The way to do this is to use NSURLRequest with NSURLConnection. If you check out the docs for NSURLConnection, they will tell you that you need to implement the callback methods in the NSURLConnectionDelegate protocol, and will give you details.
That page also points to several examples, with sample code.
You can also check out the URL Loading System Programming Guide at developer.apple.com, which will give you additional information on how these classes are intended to be used.