KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I've got the problem when I tried to do asynchronous requests to server from background thread. I've never got results of those requests. Simple example which shows the problem: @protocol AsyncImgRequestDelegate -(void) imageDownloadDidFinish:(UIImage*) img; @end @interface AsyncImgRequest : NSObject { NSMutableData* receivedData; id<AsyncImgRequestDelegate> delegate; } @property (nonatomic,retain) id<AsyncImgRequestDelegate> delegate; -(void) downloadImage:(NSString*) url ; @end @implementation AsyncImgRequest -(void) downloadImage:(NSString*) url { NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { receivedData=[[NSMutableData data] retain]; } else { } } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [delegate imageDownloadDidFinish:[UIImage imageWithData:receivedData]]; [connection release]; [receivedData release]; } @end Then I call this from main thread asyncImgRequest = [[AsyncImgRequest alloc] init]; asyncImgRequest.delegate = self; [self performSelectorInBackground:@selector(downloadImage) withObject:nil]; method downloadImage is listed below: -(void) downloadImage { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [asyncImgRequest downloadImage:@"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/l/leopard-namibia-sw.jpg"]; [pool release]; } The problem is that method imageDownloadDidFinish is never called. Moreover none of methods - (void)connectionDidFinishLoading:(NSURLConnection *)connection - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)
Tags (comma-separated)
Save Edits
Cancel