새 스레드에서 설정에 beeing 때 응답하지 않습니다연결이 연결이 새 스레드에서 설정에 beeing 때 응답하지 않는
코드 1 (미세 응답) :
[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");
}
왜이다 ? & 요청을 보내는 올바른 방법은 주 스레드/UI를 고정하지 않고 응답을 수신하는 것입니다.