views:

44

answers:

2

I know that in order to show/hide the throbber on the status bar I can use

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

But my program sends comm requests from many threads, and I need a location to control whether the throbber should be shown or hidden.

I thought about a centralized class where every comm request will register and this class will know if one-or-many requests are currently transferring bytes, and will turn the throbber on, otherwise - off.

Is this the way to go? why haven't Apple made the throbber appear automatically when networking is happening

A: 

Having some logic in your UIApplication subclass singleton seems like the natural way to handle this.

//@property bool networkThingerShouldBeThrobbingOrWhatever;
// other necessary properties, like timers
- (void)someNetworkActivityHappened {
    // set a timer or something
}
- (void)networkHasBeenQuietForABit
    // turn off the indicator.
    // UIApplcation.sharedApplication.networkActivityIndicatorVisible = NO;
}
Robert Karl
+3  A: 

Try to use something like this:

static NSInteger __LoadingObjectsCount = 0;

@interface NSObject(LoadingObjects)

+ (void) startLoad;
+ (void) stopLoad;

@end

@implementation NSObject(LoadingObjects)

+ (void) startLoad {
    __LoadingObjectsCount ++;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

+ (void) stopLoad {
    __LoadingObjectsCount--
    if ( __LoadingObjectsCount == 0 ) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    }
}

@end
Skie