I'd like to start a discussion on when to make your app multithreaded versus just using async methods or what have you.
Specifically, I ran across this situation using when I wanted to download images and put them in an imageView. I could use [NSURLConnection connectionWithRequest], or I could use [NSData dataWithContentsOfURL: ...].
The NSURLConnection approach is asynchronous, whereas datawithContentsOfURL is synchronous. However, if I do the synchronous approach in a background thread anyways, then it gives the same effect as the async request -- freeing up the UI thread.
Code:
#pragma mark --Download File with NSURLConnection--
-(void) downloadHTTPImageAsync:(NSURL*)imageURL
{
self.downloadedData = [NSMutableData data];
NSURLRequest* request = [NSURLRequest requestWithURL:imageURL];
[NSURLConnection connectionWithRequest: request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.downloadedData appendData: data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
UIImage* image = [UIImage imageWithData: self.downloadedData];
}
#pragma mark -- Download File in background thread --
-(void) downloadHTTPImageInBackground:(NSURL*)imageURL
{
[self performSelectorInBackground: @selector( _downloadHTTPImageInBackground ) withObject: imageURL waitUntilDone:NO];
}
-(void) _downloadHTTPImageInBackground:(NSURL*)imageURL
{
NSAutoReleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData* imageData = [NSData dataWithContentsOfURL: imageURL];
UIImage* image = [UIImage imageWithData: imageData];
[pool drain];
}
I didn't show it here, but it's trivial to retain the image for further use.
Thoughts? Biases of preferring one method over the other?