Last active
November 27, 2021 06:58
-
-
Save maciekish/6052297 to your computer and use it in GitHub Desktop.
UIApplication+NetworkActivity keeps track of how many network operations you currently have and manages the NetworkActivityIndicator for you.
This file contains 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
// | |
// UIApplication+NetworkActivity.h | |
// | |
// Created by Maciej Swic on 2013-04-29. | |
// Released under the MIT license. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIApplication (NetworkActivity) | |
+ (void)startNetworkActivity; | |
+ (void)finishNetworkActivity; | |
- (void)updateNetworkActivityIndicator; | |
@end |
This file contains 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
// | |
// UIApplication+NetworkActivity.m | |
// | |
// Created by Maciej Swic on 2013-04-29. | |
// Released under the MIT license. | |
// | |
#import "UIApplication+NetworkActivity.h" | |
@implementation UIApplication (NetworkActivity) | |
static NSLock *networkOperationCountLock; | |
static NSInteger networkOperationCount; | |
+ (void)startNetworkActivity { | |
[self createLock]; | |
[networkOperationCountLock lock]; | |
networkOperationCount++; | |
[networkOperationCountLock unlock]; | |
[[UIApplication sharedApplication] updateNetworkActivityIndicator]; | |
} | |
+ (void)finishNetworkActivity { | |
[self createLock]; | |
[networkOperationCountLock lock]; | |
networkOperationCount--; | |
[networkOperationCountLock unlock]; | |
[[UIApplication sharedApplication] updateNetworkActivityIndicator]; | |
} | |
+ (void)createLock { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
networkOperationCountLock = [NSLock new]; | |
}); | |
} | |
- (void)updateNetworkActivityIndicator { | |
[self setNetworkActivityIndicatorVisible:(networkOperationCount > 0 ? TRUE : FALSE)]; | |
[networkOperationCountLock lock]; | |
if (networkOperationCount < 0) { | |
networkOperationCount = 0; | |
} | |
[networkOperationCountLock unlock]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Abizern added a lock. Should be sufficient?