Skip navigation.
 
mlRe: updating progress bar from multiple nested functions - best way?
FROM : Adam Swift
DATE : Tue Apr 19 22:47:24 2005

On Apr 18, 2005, at 6:15 AM, j o a r wrote:


> On 2005-04-18, at 14.45, Johnny Deadman wrote:
>
>

>> I want to put up a progress indicator BUT the process is split up 
>> between many independent objects which don't know about each other 
>> and don't know whether the job they're doing (eg formatting a 
>> paragraph, analysing a scene) is a one-shot or part of a bigger 
>> picture.
>>
>>
>>

> If you want to have a determinate progress indicator you need to 
> know the scope of the operation in advance. It doesn't sound like 
> you have this information in your current implementation. You would 
> probably need to add some sort of pre-flight step, where you 
> compute how many separate steps are involved in the operation, and 
> perhaps also their relative "weight".
>
>
>

Another way to solve this that avoids ugly object inter-references is 
to use notifications from all of the object participating in the auto-
format process.  Based on your description of the steps and 
participants involved you could do something like:

(in FormatManager)
    // When the autoformat process starts, have FormatMananger post 
a notification
    // (if you know the scope of the autoformat tasks -eg. number of 
paragraphs-, put that
    // information into a userinfo dictionary and then as each of 
the participating)
    NSDictionary *userInfo = [NSDictionary 
dictionaryWithObject:numberOfParagraphs forKey:@"numberOfParagraphs"];
    [[NSNotificationCenter defaultCenter] 
postNotification:@"AutoFormatStartNotification" object:self 
userInfo:userInfo];

(in each of the auto-format helper objects)
    // When each step of the autoformat process completes, post a 
notification indicating the
    // step which is in progress and the percent done with that step 
(if possible)
    NSDictionary *userInfo = [NSDictionary 
dictionaryWithObjectsAndKeys:@"Formatting Paragraphs", @"stepName", 
percentDone, "percentDone", nil];
    [[NSNotificationCenter defaultCenter] 
postNotification:@"AutoFormatProgressNotification" object:self 
userInfo:userInfo];

(in FormatManager)
    // When we're done - post a notification
    [[NSNotificationCenter defaultCenter] 
postNotification:@"AutoFormatEndNotification" object:self userInfo:nil];

Then format manager or some other dedicated UI feedback controller 
can listen for the three notification names and update the progress 
bar as appropriate.

(in FormatManager or other UI feedback controller)
- (void)init... {
    ...
    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@sel(showProgressBar:) name:@"AutoFormatStartNotification" 
object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@sel(updateProgressBar:) 
name:@"AutoFormatProgressNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@sel(hideProgressBar:) name:@"AutoFormatEndNotification" 
object:nil];
    ...
}

- (void)showProgressBar:(NSNotification *)notification { // show the 
bar }
- (void)updateProgressBar:(NSNotification *)notification { // update 
the bar }
- (void)hideProgressBar:(NSNotification *)notification { // hide the 
bar }


- adam



>> It seems wrong to have to pass each object a reference to the 
>> progress bar, the proportion of the task completed, and the 
>> proportion of the task they are responsible for. It seems equally 
>> wrong to insist that each object retain a reference to the top-
>> level object and notify it. I feel they should be agnostic about 
>> the context from which they are invoked.
>>
>>
>>

> I would probably investigate using some sort of "progress 
> delegate". This would in practice be the same thing as a reference 
> to the parent, but might on a conceptual level be cleaner. By using 
> the delegate, a child could communicate it's current status, and 
> have this status propagated upwards for display to the user.
> If you operate in a context where there is no need to indicate 
> progress, you could just skip the pre-flight step and not set a 
> progress delegate.
>

Related mailsAuthorDate
mlUpdating progress bar from multiple nested functions - best way? Johnny Deadman Apr 18, 14:45
mlRe: Updating progress bar from multiple nested functions - best way? j o a r Apr 18, 15:15
mlRe: Updating progress bar from multiple nested functions - best way? John Brownlow Apr 19, 22:35
mlRe: updating progress bar from multiple nested functions - best way? Adam Swift Apr 19, 22:47