Reputation: 1091
I am having an array that contains different urls, and a set of buttons, each link is assigned to each buttons. When clicking on a button, the content in the url which is assigned to that particular button will be downloaded. The user can click on multiple buttons at the same time so that multiple download can perform at the same time. And at the same time user should have the provision to navigate through another views, so that the downloading process should not lock the UI. Which would be the best and easiest way to implement this? Please share your ideas. Thanks
Upvotes: 3
Views: 1964
Reputation: 16725
Just fetch the data asynchronously:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// The code here is executed when the response from the request comes back.
// The variable "data" will contain the response from the data.
// So you can do something like:
UIImage *anImage = [[UIImage alloc] initWithData:data]];
}];
Upvotes: 5
Reputation: 12900
You could easily implement a Asynchronous NSURLConnection
i.e. each time the user hits that button you fire up an async connection to do the dirty work.
There are plenty of examples - One of the easiest blog style examples to understand is Matt Gallagher's Cocoa With Love. Here is a link.
The gist of the technique is the delegate methods are easy to work with and you can capture each file that you download inside them.
Don't be tempted by the Synchronous style connection as it is not as flexible and you will struggle to make an easy solution for downloading multiple files using that technique.
Upvotes: 1
Reputation: 3149
Luke, use AFNetworking or ASIHTTPRequest lib with asynchronous requests.
Upvotes: 2