在 obj-c 中创建非自我委托

发布于 2024-11-02 23:01:50 字数 181 浏览 1 评论 0原文

为了在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海夕 2024-11-09 23:01:50

你有几个选择。最常见的两个是:

  • 为每个连接创建一个新类(是的,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 loaded

  • Set one class as the delegate and store references to all of your NSURLConnections. That way, when your delegate gets - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data called, you can test which NSURLConnection is being used (eg if ([connection == myConnection]) -- or whichever delegate method you're implementing)

陌若浮生 2024-11-09 23:01:50

我所做的是创建一个类来处理文件下载,并在下载完成时通过选择器通知我。我向它传递了一个委托、一个选择器和执行下载所需的信息。

- (void) downloadFileFrom:(NSString*) httpLocation respondAt:(SEL)selector on:(id)target withParam:(id)param
{
    self.finishSelector = selector;
    self.delegate = target;
    self.responseParams = param;
}

该类是它自己的 NSURLConnection 委托。因此,该实例与我可以实例化的其他实例分开,并且它负责创建自己的结果供我使用。我持有 param 对象。这可以是任何东西。

下载结束时,它会在委托上执行一个 PerformSelector: 操作。将自身传递给代表。

if ([self.target respondsToSelector:self.selector])
{
    [self.target performSelector:self.selector withObject:self.param];
}

然后您可以创建下载器的实例并调用您的方法...告诉它在哪里回复您。

MyDownloader downloader = [[MyDownloader alloc] init];
[downloader downloadFileFrom:@"http://www.mydomain.com/myimage" respondAt:@selector(myFileIsComplete:) on:self withParam: downloader];
[downloader autorelease];

另一种选择是为您的类创建一个 @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.

- (void) downloadFileFrom:(NSString*) httpLocation respondAt:(SEL)selector on:(id)target withParam:(id)param
{
    self.finishSelector = selector;
    self.delegate = target;
    self.responseParams = param;
}

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.

if ([self.target respondsToSelector:self.selector])
{
    [self.target performSelector:self.selector withObject:self.param];
}

then you can create an instance of the downloader and call your method... telling it where to reply to you.

MyDownloader downloader = [[MyDownloader alloc] init];
[downloader downloadFileFrom:@"http://www.mydomain.com/myimage" respondAt:@selector(myFileIsComplete:) on:self withParam: downloader];
[downloader autorelease];

another option is to create a @protocol for your class to respond at, and have you delegate conform to the responder.

靖瑶 2024-11-09 23:01:50

这应该可行,但还有另一种选择需要考虑。您可以创建一个通用类来创建和调用 NSURLConnection ,前提是它们足够常见。然后保留类的 NSArrayNSDictionary 。每个连接一个。

示例:我有一个应用程序需要同时下载多张照片。因此,我有一个 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 an NSArray or NSDictionary 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 the NSURLConnection and can safely set the delegate to self

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 the NSData and then returned the NSData to the calling delegate via defined success/failure protocols. It didn't care what the NSData contained.

I remodeled GetFlickrPhoto to call GenericDownload and use the returned NSData for a photo. I then made a GetJSON class that also called GenericDownload and parsed the returned NSData into a JSON feed.

Takes a bit more time but in the end you will be glad for maintenance and future projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文