Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-31-2009, 03:18 PM   #1 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default Downloading files - background thread or async?

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?
smithdale87 is offline   Reply With Quote
Old 08-31-2009, 04:34 PM   #2 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Dutch will become famous soon enough
Send a message via AIM to Dutch
Default

Interesting question. I've never used the background thread technique up to this point. Maybe run a few time tests and see how they perform. That would be my only semi-intelligent advise. I would also be interested in these results for larger images....
Dutch is offline   Reply With Quote
Old 08-31-2009, 04:48 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I can think of specific cases where I would use the async over the threaded
1. If I needed to track download progress ( this is especially useful when user may be downloading large files. It helps to know how much time is left.)
2. If I needed to track download speed. You could use this information to determine whether the network connection could handle multiple downloads at the same time.

Cons
- dataWithContentsOfURL doesnt provided an NSError if anything goes wrong. It simply returns nil.


Thats all I can think of right now.
smithdale87 is offline   Reply With Quote
Old 11-05-2011, 07:32 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 19
kpdipen is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
I can think of specific cases where I would use the async over the threaded
1. If I needed to track download progress ( this is especially useful when user may be downloading large files. It helps to know how much time is left.)
2. If I needed to track download speed. You could use this information to determine whether the network connection could handle multiple downloads at the same time.

Cons
- dataWithContentsOfURL doesnt provided an NSError if anything goes wrong. It simply returns nil.


Thats all I can think of right now.
Hey did u manage to download image in backgroundd? Can u please share ur experience?
kpdipen is offline   Reply With Quote
Old 11-05-2011, 07:41 AM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by smithdale87 View Post
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?

If you read the Apple boards, there are several Apple engineers who rail loudly and passionately about the evils of threads. Threads are very expensive to set up and run. They tie up precious physical memory for the entire life of the thread, even if your app gets moved to the background. The only way the system can use that memory is to terminate your app.

NSOperationQueue and GCD are better than NSThreads, but still make your app create more threads, which is very costly.

Apple STRONGLY encourages you to use async operations rather than threads. The system can manage the download process with dedicated system resources, rather than having to create a new thread just for your app.

Using the async NSURLConnection is also safer. Multi-threaded code requires that you know what you're doing, and can cause both subtle and spectacular problems. Multi-threaded code is also fiendishly difficult to debug. The way the threads interact is non-deterministic.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 323
11 members and 312 guests
givensur, glenn_sayers, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:29 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0