简体   繁体   English

NSURLConnection不会调用委托方法

[英]NSURLConnection doesn't call delegate methods

I saw similar questions here, but I couldn't find solution to my problem. 我在这里看到了类似的问题,但我无法找到解决问题的方法。 I have a simple NSURLConnection in main thread (At least I didn't create any other threads), but my delegate methods aren't get called 我在主线程中有一个简单的NSURLConnection(至少我没有创建任何其他线程),但是我的委托方法没有被调用

[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

and no methods called, eg 并且没有被称为的方法,例如

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse");
}

self is also a delegate for NSXMLParser, but I think it should not be a problem, as I have this working in my other class in the same project. self也是NSXMLParser的委托,但我认为它应该不是问题,因为我在同一个项目中的其他类中工作。 I checked everything 10 times already, but can't find any problem. 我已经检查了10次,但找不到任何问题。

I've seen some hack to solve it here: http://www.depl0y.com/?p=345 but I don't like it, May be someone knows better solution? 我在这里看到了一些黑客来解决它: http//www.depl0y.com/? p = 345但我不喜欢它,可能有人知道更好的解决方案吗? thanks 谢谢

The only reason I know is a separate thread (that is already terminated when the delegate methods are called). 我知道的唯一原因是一个单独的线程(在调用委托方法时已经终止)。

Try to NSLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT")); 尝试NSLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT")); right before the url connection creation 就在url连接创建之前

Try running your connection on main thread: 尝试在主线程上运行连接:

NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request
                                         delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
            forMode:NSDefaultRunLoopMode];
[connection start];

The autorelease is dangerous. 自动释放很危险。 The calls to the delegate are made after your function returns (asynchronously). 在函数返回(异步)之后调用委托。 Are you retaining it somewhere else? 你把它留在别的地方吗?

You have to release the NSURLConnection object in the - (void)connectionDidFinishLoading:(NSURLConnection *)connection callback as pointed out in the Apple documentation, not elsewhere: 您必须在Apple文档中指出的- (void)connectionDidFinishLoading:(NSURLConnection *)connection回调中release NSURLConnection对象,而不是在其他地方:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  // Do whatever you want here

  // Release the connection
  [connection release];
}

Don't release it with autorelease , as Lou Franco suggested. 请不要像autorelease那样释放它,正如Lou Franco建议的那样。

If it is not the problem, then maybe you have to implement all the required methods in the delegate class: 如果不是问题,那么您可能必须在委托类中实现所有必需的方法:

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

The delegate is retained by NSURLConnection so you don't have to worry about it. 代表由NSURLConnection保留,因此您不必担心它。

I think you may have missed NSURLConnectionDelegate in your class header file. 我想你可能已经错过了类头文件中的NSURLConnectionDelegate

For example: 例如:

@interface yourClass : NSObject <NSURLConnectionDelegate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM