2

可能重复:
管理两个 NSURLConnection
NSURLConnection 委托

我有两种不同的方法调用不同的 NSURLConnections;

方法A

NSURLConnection *serverConnection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:addressOfSideWebService]] delegate:self];

方法B

NSURLConnection *serverConnection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:addressOfMainWebService]] delegate:self];

问题是,这两个 NSURLConnections 都触发了相同的委托;

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.serverData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
    NSString *response = [[NSString alloc]initWithData:self.serverData encoding:NSUTF8StringEncoding];
    // use data
}

我想知道的是,我们可以标记 NSURLConnections 或称为 NSURLConnection 的方法,以便识别哪个函数或方法,以便我可以对其采取行动。

我们可以设置不同的代表,还是我应该遵循其他方法?

我希望我很清楚,可以提供任何其他信息。谢谢。

4

3 回答 3

5

为什么不设置如下属性:

NSURLConnection *serverConnection1;
NSURLConnection  *serverConnection2;

然后像这样使用:

serverConnection1 = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:addressOfSideWebService]] delegate:self];

然后在您的委托方法中检查它是哪个连接:

if (connection == serverConnection1) {
    //Do stuff
} else if (connection == serverConnection2) {
    //Do other stuff
}
于 2012-07-22T20:57:44.977 回答
3

您现有的代码使用“self”作为两个调用的委托,这是您问题的根源。

恕我直言,处理此问题的最佳方法是为每个响应处理程序创建单独的类。这有助于保持你的代码干净。这些类应该是包含响应处理代码的 NSURLConnectionDelegates。

当您从调用类创建 NSURLConnection 时,请确保在创建时设置正确的委托。

现在,您的每个 NSURLConnections 都会在完成后回调到它们指定的类。

所以你的调用类可能看起来像这样:

#import "FeedDownloader.h"
#import "URLConnectionResponseScenarioADelegate.h"

@implementation FeedDownloader

- (id)init {

    URLConnectionResponseScenarioADelegate *connectionDelegate = [[URLConnectionResponseScenarioADelegate alloc] init];
    NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://www.apple.com"]]  delegate:connectionDelegate];

    [myConnection start];
    return self;
}

@end

您的响应处理程序类可能如下所示:

#import "URLConnectionResponseScenarioADelegate.h"

@implementation URLConnectionResponseScenarioADelegate
@synthesize data;

#pragma mark NSURLConnection delegate methods

// The following are delegate methods for NSURLConnection. Similar to callback functions, this is how the connection object,
// which is working in the background, can asynchronously communicate back to its delegate on the thread from which it was
// started - in this case, the main thread.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    self.data = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
    [self.data appendData:_data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

}


@end
于 2012-07-22T20:46:04.710 回答
1

Radesix 提出了一个不错的方法,但它可能有点矫枉过正。解决此问题的简单方法是将每个 NSURLConncection 存储在单独的 ivar 中,并检查委托方法中的指针是否相等。

self.serverConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if(connection == self.serverConnection) {
        //Do A
    } else {
        //Do B
    }
}
于 2012-07-22T21:15:34.367 回答