Last active
August 29, 2015 14:12
-
-
Save hyperspacemark/668f093a76ad8138c82d 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
@interface NetworkActivityStatus : NSObject | |
+ (instancetype)sharedStatus; | |
- (void)pushNetworkActivity; | |
- (void)popNetworkActivity; | |
@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
#import "NetworkActivityStatus.h" | |
@interface NetworkActivityStatus () | |
@property (nonatomic) dispatch_queue_t networkActivityCounterQueue; | |
@property (nonatomic) NSUInteger networkActivityCounter; | |
@end | |
@implementation NetworkActivityStatus | |
+ (instancetype)sharedStatus | |
{ | |
static NetworkActivityStatus *status; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
status = [self new]; | |
}); | |
return status; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (!self) return nil; | |
self.networkActivityCounterQueue = dispatch_queue_create("NetworkActivityStatus Queue", NULL); | |
return self; | |
} | |
#pragma mark - Public Methods | |
- (void)pushNetworkActivity | |
{ | |
dispatch_sync(self.networkActivityCounterQueue, ^{ | |
self.networkActivityCounter++; | |
[self updateNetworkActivityIndicator]; | |
}); | |
} | |
- (void)popNetworkActivity | |
{ | |
dispatch_sync(self.networkActivityCounterQueue, ^{ | |
self.networkActivityCounter--; | |
[self updateNetworkActivityIndicator]; | |
}); | |
} | |
#pragma mark - Private Methods | |
- (void)updateNetworkActivityIndicator | |
{ | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ | |
if (self.networkActivityCounter == 0) { | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; | |
} else { | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; | |
} | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment