ios Objective-C 中的多个 NSURLConnection 委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/203294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:19:29  来源:igfitidea点击:

Multiple NSURLConnection delegates in Objective-C

iosobjective-cmacoscocoa

提问by Brian Gianforcaro

I have two NSURLConnections. The second one depends on the content of the first, so handling the data received from the connection will be different for the two connections.

我有两个 NSURLConnections。第二个依赖于第一个的内容,因此处理从连接接收到的数据对于两个连接来说是不同的。

I'm just picking up Objective-C and I would like to know what the proper way to implement the delegates is.

我只是在学习 Objective-C,我想知道实现委托的正确方法是什么。

Right now I'm using:

现在我正在使用:

NSURL *url=[NSURL URLWithString:feedURL];
NSURLRequest *urlR=[[[NSURLRequest alloc] initWithURL:url] autorelease];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:urlR delegate:self];

I don't want to use self as the delegate, how do I define two connections with different delegates?

我不想使用 self 作为委托,如何使用不同的委托定义两个连接?

NSURLConnection *c1 = [[NSURLConnection alloc] initWithRequest:url delegate:handle1];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:handle2];

How would do i create handle1 and handle2 as implementations? Or interfaces? I don't really get how you would do this.

我将如何创建 handle1 和 handle2 作为实现?还是接口?我真的不明白你会怎么做。

Any help would be awesome.

任何帮助都是极好的。

Thanks, Brian Gianforcaro

谢谢,布赖恩·吉安福卡罗

采纳答案by Ben Gottlieb

In your sample, you alloc a DownloadDelegate object without ever init'ing it.

在您的示例中,您无需初始化就分配了一个 DownloadDelegate 对象。

    DownloadDelegate *dd = [DownloadDelegate alloc];

This is dangerous. Instead:

这是危险的。反而:

    DownloadDelegate *dd = [[DownloadDelegate alloc] init];

Also, it's not strictlynecessary to declare your delegate response methods in your @interface declaration (though it won't hurt, of course). Finally, you'll want to make sure that you implement connection:didFailWithError: and connectionDidFinishLoading: to -release your DownloadDelegate object, otherwise you'll leak.

此外,在您的@interface 声明中声明您的委托响应方法并不是绝对必要的(尽管它当然不会受到伤害)。最后,你需要确保你实现了 connection:didFailWithError: 和 connectionDidFinishLoading: 来释放你的 DownloadDelegate 对象,否则你会泄漏。

Glad you're up and running!

很高兴你开始工作了!

回答by Brian Gianforcaro

Ben, while your info was helpful It didn't fully answer the question I asked.

Ben,虽然您的信息很有帮助,但它并没有完全回答我提出的问题。

I finally figured out how to setup my own delegates, which was what I was really asking.

我终于想出了如何设置我自己的代表,这正是我真正要问的。

I implemented it like so:

我是这样实现的:

@interface DownloadDelegate : NSObject 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
@end

@implementation DownloadDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
@end

We use the delegate like so:

我们像这样使用委托:

DownloadDelegate *dd = [DownloadDelegate alloc];
NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:dd];

Hope that helps anybody in the same position, and thanks again Benfor your help.

希望对处于同一位置的人有所帮助,再次感谢Ben的帮助。

Thanks,

谢谢,

Brian Gianforcaro

布赖恩·吉安福卡罗

回答by shein

I think the best way to handle multiple connections in a clean way is to keep a single delegate and just identify each NSURLConnection with a tag (it's a very VERY simple subclassing you can read about and copy from http://www.isignmeout.com/multiple-nsurlconnections-viewcontroller/)

我认为以干净的方式处理多个连接的最佳方法是保留一个委托,并用标签标识每个 NSURLConnection(这是一个非常简单的子类,您可以从http://www.isignmeout.com阅读和复制/multiple-nsurlconnections-viewcontroller/)

Basically to init every NSURLConnection with an identifying tag and then you can pull that tag in the delegate and using Switch-Case handle it according to whatever logic you need.

基本上用一个识别标签初始化每个 NSURLConnection,然后你可以在委托中拉那个标签,并根据你需要的任何逻辑使用 Switch-Case 处理它。

UPDATE

更新

I've turned the subclassed NSURLConnection into a simple Category - a little simpler and cleaner

我已经把子类 NSURLConnection 变成了一个简单的类别 - 更简单更干净

https://github.com/Shein/Categories

https://github.com/Shein/Categories

回答by Ben Gottlieb

delegates are implemented as standard NSObject-descended objects.

委托被实现为标准的 NSObject 派生对象。

You can point both connections to the same delegate.

您可以将两个连接指向同一个委托。

The delegate should implement the NSURLConnectionDelegate methods you'd like to catch (such as -connection:didReceiveData: and -connectionDidFinishLoading:). These methods will get called by the delegate as appropriate.

委托应该实现您想要捕获的 NSURLConnectionDelegate 方法(例如 -connection:didReceiveData: 和 -connectionDidFinishLoading:)。这些方法将被委托适当地调用。

回答by leonho

Try my MultipleDownload class at http://github.com/leonho/iphone-libs/tree/master, which it handles multiple NSURLConnection objects for you.

http://github.com/leonho/iphone-libs/tree/master尝试我的 MultipleDownload 类,它为您处理多个 NSURLConnection 对象。

回答by mml

since the delegates are called asynchronously, they could call didfinishloading in random order. you can then use a state check to determine if the "other" download is done yet before continuing.

由于委托是异步调用的,它们可以按随机顺序调用 didfinishloading。然后,您可以在继续之前使用状态检查来确定“其他”下载是否已完成。

i use 2 delegates:

我使用 2 个代表:

for instance (this is pseudo oc):

例如(这是伪 oc):

jsondelegate = [[JSonDelegate alloc]initWithCaller:self andSelector:@selector(jsonDone:)]
otherdelegate = [[OtherDelegate] initWithCaller:self andSelector:@selector(otherDone:)]

when each delegate finishes, the delegate informs the caller, by calling the 2 done methods.

当每个委托完成时,委托通过调用 2 个 done 方法通知调用者。

each done method receives the url data, and saves its state to an ivar. then they check to see if the other ivar is set, and continue processing if they both are done.

每个 done 方法接收 url 数据,并将其状态保存到 ivar。然后他们检查是否设置了另一个 ivar,如果它们都完成,则继续处理。

if(self.jsonString && self.otherData){
  continueProcessing
}

hope this helps.

希望这可以帮助。