Skip to content

Commit fc16648

Browse files
author
Olivier Poitrey
committed
Merge pull request #2 from mluisbrown/master
Use GCD for thread safe singleton initialisation
2 parents 74cbf5b + 33e8005 commit fc16648

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# SDNetworkActivityIndicator
2+
3+
Handle showing / hiding of the iOS network activity indicator to allow multiple concurrent threads to show / hide the indicator such that the indicator remains visible until all the requests have completed and requested the indicator to be hidden.
4+
5+
## Requirements
6+
7+
* iOS 5.0 or later.
8+
* ARC memory management.
9+
10+
## Installation
11+
12+
The easiest way to install it is by copying the following files to your project:
13+
14+
* SDNetworkActivityIndicator.h
15+
* SDNetworkActivityIndicator.m
16+
17+
## Usage
18+
19+
* When you start a network activity (will show the network activity indicator):
20+
21+
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
22+
23+
* When you finish a network activity (will hide the network activity indicator only if the number of calls to `stopActivity` matches the number of calls to `startActivity`):
24+
25+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
26+
27+
* To hide the network activity indicator regardless of whether all activities have finished (without having to call `stopActivity` for each `startActivity` called):
28+
29+
[[SDNetworkActivityIndicator sharedActivityIndicator] stopAllActivity];
30+
31+
32+
## License
33+
Copyright (c) 2010 Olivier Poitrey <rs@dailymotion.com>
34+
35+
Permission is hereby granted, free of charge, to any person obtaining a copy
36+
of this software and associated documentation files (the "Software"), to deal
37+
in the Software without restriction, including without limitation the rights
38+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39+
copies of the Software, and to permit persons to whom the Software is furnished
40+
to do so, subject to the following conditions:
41+
42+
The above copyright notice and this permission notice shall be included in all
43+
copies or substantial portions of the Software.
44+
45+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
51+
THE SOFTWARE.
52+

SDNetworkActivityIndicator.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#import <Foundation/Foundation.h>
1010

1111
@interface SDNetworkActivityIndicator : NSObject
12-
{
13-
@private
14-
NSUInteger counter;
15-
}
1612

1713
+ (id)sharedActivityIndicator;
1814
- (void)startActivity;

SDNetworkActivityIndicator.m

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88

99
#import "SDNetworkActivityIndicator.h"
1010

11-
static SDNetworkActivityIndicator *instance;
11+
@interface SDNetworkActivityIndicator()
12+
{
13+
@private NSUInteger counter;
14+
}
15+
@end
16+
1217

1318
@implementation SDNetworkActivityIndicator
1419

15-
+ (id)sharedActivityIndicator
20+
+ (instancetype) sharedActivityIndicator
1621
{
17-
if (instance == nil)
18-
{
19-
instance = [[SDNetworkActivityIndicator alloc] init];
20-
}
21-
22-
return instance;
22+
static id _sharedInstance = nil;
23+
static dispatch_once_t onceToken;
24+
dispatch_once(&onceToken, ^{
25+
_sharedInstance = [[self alloc] init];
26+
});
27+
28+
return _sharedInstance;
2329
}
2430

2531
- (id)init
@@ -36,11 +42,8 @@ - (void)startActivity
3642
{
3743
@synchronized(self)
3844
{
39-
if (counter == 0)
40-
{
41-
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
42-
}
4345
counter++;
46+
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
4447
}
4548
}
4649

0 commit comments

Comments
 (0)