3

I am trying show a UIProgressView while downloading an image from a server by using ASIHTTPRequest. I want to show progress on the UIProgressView which is working perfectly using this code:

[request setDownloadProgressDelegate:progressIndicator];

I want to show % right next to my progress bar, so the user knows what percentage has been downloaded. Some thing like this (I made this in Photoshop):

mockup of desired UI


What I thought of is to use

- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes;

to update my UILabel, This method is always called when ASI request receives some amount of bytes.

But to call this method I have to implement it in my class and then setDownloadProgressDelegate to self, like this,

[request setDownloadProgressDelegate:self];

Therefore, can I make the request object have two different delegates at the same time, so I can receive responses with both? I just need to do some math to calculate the percentage and update my UILabel -- I know the exact size of the file I am downloading.

2
  • 2
    I haven't actually tried it myself, but could you use KVO to observe the float value of the progress bar, and update the label appropriately? Not sure if that's possible, though. Nov 1, 2011 at 2:45
  • I found the easier way late I can just call [request setDelegate:self]; and [request setDownloadProgressDelegate:myProgressViewObject]; to solve my problem. Thanks btw learnt new thing about KVO
    – TeaCupApp
    Nov 2, 2011 at 5:49

3 Answers 3

3

You want to use NSNotificationCenter. This allows you to loosely couple objects using the pub/sub model. You can post a Notification from an object and any arbitrary number of objects can get the information, without the tight coupling of delegates.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

1
  • Thanks, this was helpful but later I found an easier way to acheive it :)
    – TeaCupApp
    Nov 3, 2011 at 1:03
2

Any object can have more than one delegate, you'll just have to keep track of them. So you could have two separate protocols for your two different scenarios, and two id variables for each delegate. This is similar to what UITableView does with its delegate and datasource, which can be two different objects.

1
  • Thanks, this was helpful but later I found an easier way to acheive it :)
    – TeaCupApp
    Nov 3, 2011 at 1:02
2

After few hours of reading I finally found a easiest way to solve this problem, ASIHTTPRequest has two different method which can help to set delegates.

  1. For progress view indicator. - This updates Progress view in short increments it.
  2. For class it self. - And this allows to add method in class which called by ASIHTTPRequest object while it finishes it's downloading or uploading.

So what I did is I set my progress view delegate and than I set my self as a delegate of request object. like,

[request setDownloadProgressDelegate:myProgressViewObject]; // For progress view increment
[request setDelegate:self]; // For adding methods such as finished downloading started downloading.

Mt purpose was to let my request object call below method,

- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes; // Now I can increment my UILabel percentage here.

Which is messaged periodically by setting,

[request setDelegate:self]; 

Hope this will help others in future!

Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.