Iphone 第二个异步NSURLConnection不';t调用委托方法

Iphone 第二个异步NSURLConnection不';t调用委托方法,iphone,objective-c,delegates,nsurlconnection,nsurlrequest,Iphone,Objective C,Delegates,Nsurlconnection,Nsurlrequest,在我的ApplicationLegate中,我正在通过网络进行NSURLConnection获取(它被包装在一个类中,如下所示)。这一个似乎工作正常:我获得了didReceiveData中的所有数据,并获得了完成调用ConnectiondFinishLoading。在ConnectionIDFinishLoading的末尾,我实例化了一个或多个稍微不同的包装类,但它们本质上是相同的。问题是第二个NSURLConnection的委托从未调用过它的方法 我看过了,但都没用。我没有生成任何新线程,我在

在我的ApplicationLegate中,我正在通过网络进行NSURLConnection获取(它被包装在一个类中,如下所示)。这一个似乎工作正常:我获得了didReceiveData中的所有数据,并获得了完成调用ConnectiondFinishLoading。在ConnectionIDFinishLoading的末尾,我实例化了一个或多个稍微不同的包装类,但它们本质上是相同的。问题是第二个NSURLConnection的委托从未调用过它的方法

我看过了,但都没用。我没有生成任何新线程,我在整个代码中乱丢的所有[NSThread isMainThread]检查都返回true

我被难住了。有人能帮我吗?以下是相关代码:

应用程序代表:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ConnectionWrapper* w = [[ConnectionWrapper alloc] initWithParams:self
    url:[NSURL URLWithString:<url>]];
[w beginFetch];

    return YES;
}

...

-(void)fetchCompleted:(NSURL*)url directory:(NSString*)directory
{
NSLog(@"fetch completed");
}

-(void)fetchFailed:(NSURL*)url
{
NSLog(@"fetch failed");
}

...
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
ConnectionWrapper*w=[[ConnectionWrapper alloc]initWithParams:self
url:[NSURL URLWithString:];
[w beginFetch];
返回YES;
}
...
-(void)fetchCompleted:(NSURL*)url目录:(NSString*)目录
{
NSLog(@“获取完成”);
}
-(void)获取失败:(NSURL*)url
{
NSLog(@“获取失败”);
}
...
连接包装器:

-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url
{
    delegate = d;

    connURL = url;

    return [self init];
}

-(void)beginFetch
{
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL];
    NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [conn release];
    [request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"append");
    [responseData appendData:data];
}

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

    DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self
        url:[NSURL URLWithString:<different url>]];
    [w beginFetch];
}

-(void)fetchCompleted:(NSURL*)URL
{
    NSLog(@"completed: %@", URL);
}

-(void)fetchFailed:(NSURL*)URL
{
    NSLog(@"failed");
}
-(id)initWithParams:(id)d url:(NSURL*)url
{
代表=d;
connURL=url;
返回[自初始化];
}
-(无效)beginFetch
{
NSURLRequest*request=[[NSURLRequest alloc]initWithURL:connURL];
NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:request委托:self];
[控制释放];
[请求释放];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
NSLog(@“附加”);
[应答数据:数据];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
…解析。。。。
DifferenceConnectionWrapper*w=[[DifferenceConnectionWrapper alloc]initWithParams:self
url:[NSURL URLWithString:];
[w beginFetch];
}
-(void)fetchCompleted:(NSURL*)URL
{
NSLog(@“已完成:%@”,URL);
}
-(void)获取失败:(NSURL*)URL
{
NSLog(@“失败”);
}
差异连接包装器:

-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url
{
    delegate = d;

    connURL = url;

    return [self init];
}

-(void)beginFetch
{
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL];
    NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [conn release];
    [request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"append");
    [responseData appendData:data];
}

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

    DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self
        url:[NSURL URLWithString:<different url>]];
    [w beginFetch];
}

-(void)fetchCompleted:(NSURL*)URL
{
    NSLog(@"completed: %@", URL);
}

-(void)fetchFailed:(NSURL*)URL
{
    NSLog(@"failed");
}
-(id)initWithParams:(id)d url:(NSURL*)url { 代表=d

    connURL = url;

    return [self init];
}

-(void)beginFetch
{
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL];
    NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [conn release];
    [request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"append");
    [responseData appendData:data];
}

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

    DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self
        url:[NSURL URLWithString:<different url>]];
    [w beginFetch];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"got response");
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"got data");
    [responseData appendData:data];
}

- (void) connectionDidFinishLoading: (NSURLConnection*) connection
{
    NSLog(@"image saver completed: %@", connURL);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error");
}
connURL=url;
返回[自初始化];
}
-(无效)beginFetch
{
NSURLRequest*request=[[NSURLRequest alloc]initWithURL:connURL];
NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:request委托:self];
[控制释放];
[请求释放];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
NSLog(@“附加”);
[应答数据:数据];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
…解析。。。。
DifferenceConnectionWrapper*w=[[DifferenceConnectionWrapper alloc]initWithParams:self
url:[NSURL URLWithString:];
[w beginFetch];
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
NSLog(@“得到响应”);
[响应数据设置长度:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
NSLog(@“获取数据”);
[应答数据:数据];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
NSLog(@“图像保护程序已完成:%@”,connURL);
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
NSLog(@“错误”);
}
ConnectionWrapper和DifferenceConnectionWrapper具有类似的功能,但为了简洁起见,我在这里省略了其他逻辑


谢谢你的帮助。非常感谢。

我认为问题在于您正在
-beginFetch
中释放URL连接:

-(void)beginFetch
{
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL];
    NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [conn release];
    [request release];
}
URL连接对象应保持活动状态,并在连接完成加载后释放:

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

    // *** Release the connection and whatever data you’ve kept related to
    // this particular connection
    [connection release];
    [responseData release];
    // *** or [responseData setLenght:0]; depending on how you’re
    // managing responseData

    DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self
        url:[NSURL URLWithString:<different url>]];
    [w beginFetch];
}

编辑:您的初始化器有点奇怪:

-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url
{
    delegate = d;    
    connURL = url;
    return [self init];
}

不要忘记在
-dealloc
中释放
url
对象,或者在为
connURL
分配另一个值时,有几件事:我没有看到didfailwither错误:在您的第一个包装类中,(有点离题)您是否使用不同的连接包装器泄漏内存*w

无论如何,我要尝试的是:看看是否可以直接从appDelegate而不是ConnectionWrapper调用不同的ConnectionWrapper。 我会尝试在任何情况下分离这两个调用。当第一个完成并调用appDelegate时,您不能从那里启动不同的ConnectionWrapper吗


我知道这并不能解释你的问题,但你可以让它工作起来(这两件事中哪一件更重要,是一个完全不同的争论)。原来这个bug是由我错过的东西引起的。在第二次请求之后,我马上陷入了困境,这可能会把任何事情搞砸。一旦我解决了那个问题,一切都很好


谢谢你的帮助。

看起来他确实在泄露他的连接包装。DifferenticConnectionWrapper可以从应用程序代理中独立工作,但是一旦我链接了两个连接请求,第二个就没有用了。我知道泄漏:这只是我为调试所做的事情(我想消除任何保留/发布问题)。不保留对NSURLConnection的引用没有错。它将被runloop保留。@fluchtpunkt是否有此文档记录?现在找不到它。我以这种方式使用NSURLConnections已经很长时间了,所以我希望我没有错。但是我找不到文档。@fluchtpunkt-Hmm,我已经检查了运行循环输入源的文档,我没有找到任何关于运行循环保留其源的提及,除非它们是计时器。如果你碰巧发现这件事,请告诉我。
-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url
{
    self = [super init];
    if (self) {
        delegate = d;    
        connURL = [url retain];
    }
    return self;
}