|
// |
|
// RBNetworkActivityIndicatorManager.m |
|
// |
|
// Copyright (c) 2011 Robert Brown |
|
// |
|
// Permission is hereby granted, free of charge, to any person obtaining a copy |
|
// of this software and associated documentation files (the "Software"), to deal |
|
// in the Software without restriction, including without limitation the rights |
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
// copies of the Software, and to permit persons to whom the Software is |
|
// furnished to do so, subject to the following conditions: |
|
// |
|
// The above copyright notice and this permission notice shall be included in |
|
// all copies or substantial portions of the Software. |
|
// |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
// THE SOFTWARE. |
|
// |
|
|
|
#import <dispatch/dispatch.h> |
|
|
|
#import "RBNetworkActivityIndicatorManager.h" |
|
|
|
/// Shared instance. |
|
static RBNetworkActivityIndicatorManager * sharedManager = nil; |
|
|
|
|
|
@interface RBNetworkActivityIndicatorManager () |
|
|
|
/// The number of tasks interested in showing the network activity indicator. |
|
@property (nonatomic, assign) NSInteger * activityCount; |
|
|
|
/// User queue that serializes all requests. |
|
@property (nonatomic, assign) dispatch_queue_t activityQueue; |
|
|
|
/** |
|
* Private initializer. |
|
* |
|
* @return self; |
|
*/ |
|
- (id)initialize; |
|
|
|
@end |
|
|
|
|
|
@implementation RBNetworkActivityIndicatorManager |
|
|
|
@synthesize activityCount, activityQueue; |
|
|
|
- (void)startActivity { |
|
|
|
// Puts the request on the serial queue to run when convenient. |
|
dispatch_async([self activityQueue], ^{ |
|
|
|
[self setActivityCount:[self activityCount] + 1]; |
|
|
|
// Showing the activity indicator probably isn't threadsafe. |
|
// The call is synchronous so all requests are serialized. |
|
dispatch_sync(dispatch_get_main_queue(), ^{ |
|
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; |
|
}); |
|
}); |
|
} |
|
|
|
- (void)stopActivity { |
|
|
|
// Puts the request on the serial queue to run when convenient. |
|
dispatch_async([self activityQueue], ^{ |
|
|
|
[self setActivityCount:[self activityCount] - 1]; |
|
|
|
// Hiding the activity indicator probably isn't threadsafe. |
|
// The call is synchronous so all requests are serialized. |
|
if ([self activityCount] == 0) |
|
dispatch_sync(dispatch_get_main_queue(), ^{ |
|
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; |
|
}); |
|
|
|
NSAssert([self activityCount] >= 0, @"Invalid activity count."); |
|
}); |
|
} |
|
|
|
+ (void)startActivity { |
|
[[self sharedManager] startActivity]; |
|
} |
|
|
|
+ (void)stopActivity { |
|
[[self sharedManager] stopActivity]; |
|
} |
|
|
|
|
|
#pragma mark - Singleton methods |
|
|
|
+ (RBNetworkActivityIndicatorManager *)sharedManager { |
|
|
|
// Technically singletons are memory leaks, but we don't want the static |
|
// analyzer to know that. |
|
#if !defined(__clang_analyzer__) |
|
|
|
@synchronized(self) { |
|
if (!sharedManager) |
|
sharedManager = [[super allocWithZone:nil] initialize]; |
|
} |
|
|
|
#endif |
|
|
|
return sharedManager; |
|
} |
|
|
|
- (id)initialize { |
|
|
|
[self setActivityCount:0]; |
|
|
|
// Creates a user queue to serialize all the requests. This way we don't |
|
// need locks or semaphores. The queue is also targeted to the low priority |
|
// global queue since we aren't doing anything terribly important. |
|
dispatch_queue_t queue = dispatch_queue_create("com.RobertBrown.ActivityQueue", 0); |
|
dispatch_set_target_queue(queue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)); |
|
[self setActivityQueue:queue]; |
|
|
|
return self; |
|
} |
|
|
|
+ (id)allocWithZone:(NSZone *)zone { |
|
|
|
// The retain is needed to satisfy the static analyzer. |
|
return [[self sharedManager] retain]; |
|
} |
|
|
|
- (id)copyWithZone:(NSZone *)zone { |
|
return self; |
|
} |
|
|
|
- (id)init { |
|
return self; |
|
} |
|
|
|
- (id)retain { |
|
return self; |
|
} |
|
|
|
- (oneway void)release { |
|
// Do nothing. |
|
} |
|
|
|
- (id)autorelease { |
|
return self; |
|
} |
|
|
|
- (NSUInteger)retainCount { |
|
return NSUIntegerMax; |
|
} |
|
|
|
@end |