Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 alloc] initWithRequest:request delegate:self] autorelease];

and no methods called, e.g.

- (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. I checked everything 10 times already, but can't find any problem.

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? thanks

like image 784
Burjua Avatar asked Jul 23 '10 15:07

Burjua


2 Answers

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"));right before the url connection creation

like image 189
Michael Kessler Avatar answered Oct 14 '22 21:10

Michael Kessler


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];
like image 40
Art Avatar answered Oct 14 '22 20:10

Art