How to control network activity indicator on iPhone

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

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

But my program sends commands from many threads, and I need a place to control whether throbber should be shown or hidden.

I thought of a centralized class in which every compilation request will be registered, and this class will know if one or many requests are currently transmitting bytes, and turn on throbber, otherwise turn it off.

Is that the way? Why didn’t Apple make the trombone automatically when the network happens?

+4
source share
2 answers

Try using something like this:

 static NSInteger __LoadingObjectsCount = 0; @interface NSObject(LoadingObjects) + (void)startLoad; + (void)stopLoad; + (void)updateLoadCountWithDelta:(NSInteger)countDelta; @end @implementation NSObject(LoadingObjects) + (void)startLoad { [self updateLoadCountWithDelta:1]; } + (void)stopLoad { [self updateLoadCountWithDelta:-1]; } + (void)updateLoadCountWithDelta:(NSInteger)countDelta { @synchronized(self) { __LoadingObjectsCount += countDelta; __LoadingObjectsCount = (__LoadingObjectsCount < 0) ? 0 : __LoadingObjectsCount ; [UIApplication sharedApplication].networkActivityIndicatorVisible = __LoadingObjectsCount > 0; } } 

UPDATE: Made in streaming mode

+7
source

Having some logic in your UIApplication singleton subclass seems like a 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; } 
0
source

All Articles