Mithuzz
Mithuzz

Reputation: 1091

UIProgressView with multiple asynchronous NSURLConnection in iOs

I am using following code to download file from url's asynchronously,

 NSMutableData *responseData = [[NSMutableData alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://www.tuiscos.com/images/trading.png"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request 
                                           queue:[NSOperationQueue mainQueue] 
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                   // do something with data
                                   [responseData appendData:data];
                                   myImage8.image = [UIImage imageWithData:data];
                                   NSInteger len = response.expectedContentLength;
                                   NSInteger receiverdBytes = 0;
                                   receiverdBytes = data.length+ receiverdBytes;
                                   float prog = (float)[responseData length]/(float)len;
                                   [progress8 setProgress:prog];
                               }];

as the download progresses, I want to update the progress bar, but using this code, I am not getting a gradual progress, instead it is waiting to complete the download and jumping to the maximum value. How can I make a gradual progress in the value? Can somebody provide a sample code? For asynchronous method with delegate methods. Thanks :)

Upvotes: 1

Views: 2177

Answers (2)

butters
butters

Reputation: 164

If you don't want to code everything on your own, I would suggest using ASIHTTPRequesst on this task:

http://allseeing-i.com/ASIHTTPRequest/How-to-use

It is very simple to implement and you can do simultaneous, asynchrony downloads. It also provides delegates for all needs, also for progress updates.

I used it in my projects for almost a year now and never regretted it.

Upvotes: 1

Gabriel
Gabriel

Reputation: 3359

CompletionHandler is executed at completion, of course. You have to a delegate for the connection. Use -initWithRequest:delegate: method. You will have to code the NSURLConnectionDelegate methods and the one you need to set progressView value is -connection:didReceiveData:

Here is the doc: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.pdf

Upvotes: 0

Related Questions