Skip to content

Instantly share code, notes, and snippets.

@jackhl
Created July 12, 2012 02:21
Show Gist options
  • Save jackhl/3095255 to your computer and use it in GitHub Desktop.
Save jackhl/3095255 to your computer and use it in GitHub Desktop.
//
// JLNetworkActivityIndicatorManager.h
// EcoCr
//
// Created by Jack Lawrence on 6/27/12.
//
//
#import <Foundation/Foundation.h>
/** Manages the animation state of the network activity indicator in the top left of the screen. Threadsafe. */
@interface JLNetworkActivityIndicatorManager : NSObject
/** Designated Initializer */
/**
Returns a shared instance of `JLNetworkActivityIndicatorManager`.
@return A shared instance of `JLNetworkActivityIndicatorManager`
*/
+ (id)sharedManager;
/** Increments the number of network connections that are currently active. */
- (void)increment;
/** Decrements the number of network connections that are currently active. */
- (void)decrement;
@end
//
// JLNetworkActivityIndicatorManager.m
// EcoCr
//
// Created by Jack Lawrence on 6/27/12.
//
//
#import "JLNetworkActivityIndicatorManager.h"
@implementation JLNetworkActivityIndicatorManager {
NSInteger _activeNetworkRequests;
dispatch_queue_t _serialQueue;
}
+ (id)sharedManager
{
__strong static id _sharedObject = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id)init
{
self = [super init];
if (self)
{
_serialQueue = dispatch_queue_create("com.McGeehan.EcoCr.netIndicatorManager", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)dealloc
{
dispatch_release(_serialQueue);
}
- (void)increment
{
dispatch_async(_serialQueue, ^{
if (_activeNetworkRequests == 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
_activeNetworkRequests += 1;
});
}
- (void)decrement
{
dispatch_async(_serialQueue, ^{
if (_activeNetworkRequests > 0) {
_activeNetworkRequests -= 1;
if (_activeNetworkRequests <= 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment