Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A threadsafe network activity indicator manager.

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.

//
// 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
//
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.