NSURLConnection委托 - 确定触发委托的功能或连接

时间:2012-07-22 20:34:10

标签: iphone ios delegates nsurlconnection

  

可能重复:
  Managing two NSURLConnection
  NSURLConnection delegate

我有两种不同的方法可以调用不同的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的方法,以便识别我可以对其采取行动的功能或方法。

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

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

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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

答案 2 :(得分: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
    }
}