Created
July 12, 2012 02:21
-
-
Save jackhl/3095255 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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