Hi,
The connection doesn't respond when beeing set from a new thread:
Code 1 (responds fine):
[self setConnection];
}
- (void)setConnection{
NSLog(@"setting myConnection with request");
myConnection = [[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:requestURL] delegate:self] autorelease];
}
Log 1:
2010-02-25 10:44:04.384 Untitled[1002:207] setting myConnection with request
2010-02-25 10:44:06.093 Untitled[1002:207] didReceiveResponse
2010-02-25 10:44:06.094 Untitled[1002:207] didReceiveData
2010-02-25 10:44:06.094 Untitled[1002:207] DidFinishLoading
Code 2:
[NSThread detachNewThreadSelector:@selector(setConnection) toTarget:self withObject:nil];
}
- (void)setConnection{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"setting myConnection with request");
myConnection = [[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:requestURL] delegate:self] autorelease];
[pool release];
}
Log 2:
2010-02-25 10:40:50.280 Untitled[972:4003] setting myConnection with request
Delegates:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"DidFinishLoading");
}
Why is that? What is the correct way for sending a request & receiving a response- without freezing the main thread / UI..
Thanks-
Nir.