在 obj-c 中创建非自我委托
为了在 Objective c 中使用异步 http 请求,您需要为 NSURLConnection
设置委托。问题是我需要发出多个 http 请求,因此拥有相同的委托(自身)是行不通的。
解决这个问题的最佳方法是什么?我应该为每个 http 请求创建一个新的委托类吗?这些委托只是 NSObject 吗?
In order to use asynchronous http requests in objective c, you need to set a delegate to NSURLConnection
. The problem is that I need to make multiple http requests, so having the same delegate (self) wont work.
What is the best way to go about this? Should I make a new delegate class for each http request? Are these delegates just NSObjects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有几个选择。最常见的两个是:
为每个连接创建一个新类(是的,
NSObject
的子类)并将它们设置为委托 - 让它们在数据传输时执行您需要的任何逻辑已加载将一个类设置为委托并存储对所有
NSURLConnection
的引用。这样,当您的委托调用- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
时,您可以测试正在使用哪个NSURLConnection
(例如if ([connection == myConnection])
-- 或您正在实现的任何委托方法)You have a few options. The two most most common are:
Make a new class for each connection (yes, a subclass of
NSObject
) and set them as delegates -- have them carry out whatever logic you need when the data is loadedSet one class as the delegate and store references to all of your
NSURLConnection
s. That way, when your delegate gets- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
called, you can test whichNSURLConnection
is being used (egif ([connection == myConnection])
-- or whichever delegate method you're implementing)我所做的是创建一个类来处理文件下载,并在下载完成时通过选择器通知我。我向它传递了一个委托、一个选择器和执行下载所需的信息。
该类是它自己的 NSURLConnection 委托。因此,该实例与我可以实例化的其他实例分开,并且它负责创建自己的结果供我使用。我持有 param 对象。这可以是任何东西。
下载结束时,它会在委托上执行一个 PerformSelector: 操作。将自身传递给代表。
然后您可以创建下载器的实例并调用您的方法...告诉它在哪里回复您。
另一种选择是为您的类创建一个 @protocol 来响应,并让您委托符合响应者。
What I do is make a class that will handle downloading a file, and notify me when it is done through a selector. I pass it a delegate, a selector and the Info it needs to perform the download.
the Class is its own NSURLConnection delegate. Therefore the instance is separated from the others that I may instantiate, And it handles creating its own result for me to work with. I hold onto the param object. which could be anything.
At the end of the download it does a performSelector: on the delegate. passing itself to the delegate.
then you can create an instance of the downloader and call your method... telling it where to reply to you.
another option is to create a @protocol for your class to respond at, and have you delegate conform to the responder.
这应该可行,但还有另一种选择需要考虑。您可以创建一个通用类来创建和调用
NSURLConnection
,前提是它们足够常见。然后保留类的NSArray
或NSDictionary
。每个连接一个。示例:我有一个应用程序需要同时下载多张照片。因此,我有一个
GetFlickrPhoto
类。它有一个自定义 init 方法,用于接收 URL 和任何其他必要的信息。每个单独的类都会创建NSURLConnection
并可以安全地将委托设置为self
这有助于保持事物的可控性并且非常易于管理/可重用。
更进一步:
我之前提到的应用程序也需要下载 JSON feed。因此,我创建了一个 GenericDownload 类,它接收 URL 并异步下载 NSData,然后通过定义的成功/失败将 NSData 返回给调用委托协议。它并不关心
NSData
包含什么。我重新构建了
GetFlickrPhoto
以调用GenericDownload
并使用返回的NSData
来获取照片。然后,我创建了一个也称为 GenericDownload 的 GetJSON 类,并将返回的 NSData 解析为 JSON 提要。需要更多时间,但最终您会对维护和未来的项目感到高兴。
That should work, but there is another option to consider. You could make a generic class that creates and calls the
NSURLConnection
provided they are common enough. Then keep anNSArray
orNSDictionary
of the classes. One for each connection.Example: I have an app that needs to download several photos simultaneously. Therefore, I have a
GetFlickrPhoto
class. It has a custom init method that receives the URL and any other necessary info. Each individual class creates theNSURLConnection
and can safely set the delegate toself
This helps keep things contained and very manageable/reusable.
To take it a step further:
The app I mentioned before, also needed to download JSON feeds. So I made a
GenericDownload
class that took in URL and asynchronously downloaded theNSData
and then returned theNSData
to the calling delegate via defined success/failure protocols. It didn't care what theNSData
contained.I remodeled
GetFlickrPhoto
to callGenericDownload
and use the returnedNSData
for a photo. I then made aGetJSON
class that also calledGenericDownload
and parsed the returnedNSData
into a JSON feed.Takes a bit more time but in the end you will be glad for maintenance and future projects.