NSURLConnection不会调用委托方法

Bur*_*jua 15 iphone delegates nsurlconnection

我在这里看到了类似的问题,但我无法找到解决问题的方法.我在主线程中有一个简单的NSURLConnection(至少我没有创建任何其他线程),但是我的委托方法没有被调用

[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
Run Code Online (Sandbox Code Playgroud)

并且没有被称为的方法,例如

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse");
}
Run Code Online (Sandbox Code Playgroud)

self也是NSXMLParser的委托,但我认为它不应该是一个问题,因为我在同一个项目中的其他类中工作.我已经检查了10次,但找不到任何问题.

我在这里看到了一些黑客来解决它:http://www.depl0y.com/? p = 345 但我不喜欢它,可能有人知道更好的解决方案吗?谢谢

Mic*_*ler 27

我知道的唯一原因是一个单独的线程(在调用委托方法时已经终止).

尝试NSLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT"));在url连接创建之前


Art*_*Art 15

尝试在主线程上运行连接:

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

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
            forMode:NSDefaultRunLoopMode];
[connection start];
Run Code Online (Sandbox Code Playgroud)