public

A threadsafe network activity indicator manager.

  • Download Gist
RBNetworkActivityIndicatorManager.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
//
// RBNetworkActivityIndicatorManager.h
//
// 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 <Foundation/Foundation.h>
 
/**
* Simple singleton for managing the network activity indicator. Works just like
* memory management. If you start the activity indicator you are responsible
* for stopping it. Only when all activity is stopped will the network activity
* indicator turn off. Best of all, this singleton is threadsafe.
*/
@interface RBNetworkActivityIndicatorManager : NSObject
 
/**
* Indicates that a thread wants the network activity indicator visible.
* Threadsafe.
*/
- (void)startActivity;
 
/**
* Indicates that a thread is no longer interested in the network activity
* indicator's visiblility. Threadsafe.
*/
- (void)stopActivity;
 
/**
* Convenience method. Indicates that a thread wants the network activity
* indicator visible. Threadsafe.
*/
+ (void)startActivity;
 
/**
* Convenience method. Indicates that a thread is no longer interested in the
* network activity indicator's visiblility. Threadsafe.
*/
+ (void)stopActivity;
 
/**
* Rteturns the shared instance.
*
* @return The shared instance.
*/
+ (RBNetworkActivityIndicatorManager *)sharedManager;
 
@end
RBNetworkActivityIndicatorManager.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
//
// 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
README.markdown
Markdown

RBNetworkActivity

Summary

RBNetworkActivity is a threadsafe network activity indicator manager.

Dependencies

RBNetworkActivity uses Grand Central Dispatch (AKA GCD or libdispatch) for type safety.

RBNetworkActivity is written for iOS 4.0+ support.

How To use

RBNetworkActivityIndicatorManager works just like memory management. If you start a network activity, you are responsible for stopping it. Using this class is easy, there are only two methods: +startActivity and +stopActivity (see example below).

[RBNetworkActivityIndicatorManager startActivity];

// Some network task.

[RBNetworkActivityIndicatorManager stopActivity];

License

RBReporter is licensed under the MIT license, which is reproduced in its entirety here:

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.