Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Internally, use NSOperationQueue, rather than libdispatch

  • Loading branch information...
commit a6641e23edfe488318522fbdefd8a394ba11fd5c 1 parent 2850fae
Daniel Tull authored June 25, 2012
66  DCTNetworkActivityIndicatorController/DCTNetworkActivityIndicatorController.m
@@ -10,27 +10,19 @@
10 10
 #import "DCTNetworkActivityIndicatorController.h"
11 11
 
12 12
 // These tie to DCTConnectionQueue
13  
-NSString *const DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification = @"DCTConnectionQueueActiveConnectionCountIncreasedNotification";
14  
-NSString *const DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification = @"DCTConnectionQueueActiveConnectionCountDecreasedNotification";
15  
-
16  
-NSString *const DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey = @"networkActivity";
  13
+NSString *const DCTNetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification = @"DCTConnectionQueueActiveConnectionCountIncreasedNotification";
  14
+NSString *const DCTNetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification = @"DCTConnectionQueueActiveConnectionCountDecreasedNotification";
17 15
 
  16
+NSString *const DCTNetworkActivityIndicatorControllerNetworkActivityKey = @"networkActivity";
18 17
 NSString *const DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification = @"DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification";
19 18
 
20 19
 static DCTNetworkActivityIndicatorController *sharedInstance = nil;
21 20
 
22  
-@interface DCTNetworkActivityIndicatorController ()
23  
-- (void)dctInternal_calculateNetworkActivity;
24  
-@end
25  
-
26 21
 @implementation DCTNetworkActivityIndicatorController {
27  
-	dispatch_queue_t _queue;
  22
+	__strong NSOperationQueue *_operationQueue;
28 23
 }
29 24
 
30  
-@synthesize networkActivity;
31  
-
32 25
 + (void)load {
33  
-	
34 26
 	@autoreleasepool {
35 27
 	    [DCTNetworkActivityIndicatorController sharedNetworkActivityIndicatorController];
36 28
 	}
@@ -50,51 +42,49 @@ - (id)init {
50 42
     
51 43
     if (!(self = [super init])) return nil;
52 44
     
53  
-	networkActivity = 0;
  45
+	_networkActivity = 0;
54 46
 	
55 47
 	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
56  
-	[notificationCenter addObserver:self selector:@selector(incrementNetworkActivity) name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification object:nil];
57  
-	[notificationCenter addObserver:self selector:@selector(decrementNetworkActivity) name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification object:nil];
58  
-	_queue = dispatch_queue_create("uk.co.danieltull.DCTNetworkActivityIndicatorController", NULL);
  48
+	[notificationCenter addObserver:self selector:@selector(incrementNetworkActivity) name:DCTNetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification object:nil];
  49
+	[notificationCenter addObserver:self selector:@selector(decrementNetworkActivity) name:DCTNetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification object:nil];
  50
+	_operationQueue = [NSOperationQueue new];
  51
+	[_operationQueue setMaxConcurrentOperationCount:1];
  52
+	[_operationQueue setName:@"uk.co.danieltull.DCTNetworkActivityIndicatorController"];
59 53
 	
60 54
 	return self;
61 55
 }
62 56
 
63 57
 - (void)dealloc {
64  
-	dispatch_release(_queue);
65 58
 	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
66  
-	
67  
-	[notificationCenter removeObserver:self name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification object:nil];
68  
-	[notificationCenter removeObserver:self name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification object:nil];
  59
+	[notificationCenter removeObserver:self name:DCTNetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification object:nil];
  60
+	[notificationCenter removeObserver:self name:DCTNetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification object:nil];
69 61
 }
70 62
 
71 63
 - (void)decrementNetworkActivity {
72 64
 	
73  
-	dispatch_async(_queue, ^{
74  
-		NSAssert((networkActivity > 0), @"%@ increment/decrement calls mismatch", self);
75  
-		[self willChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
76  
-		networkActivity--;
77  
-		[self didChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
  65
+	[_operationQueue addOperationWithBlock:^{
  66
+		NSAssert((self.networkActivity > 0), @"%@ increment/decrement calls mismatch", self);
  67
+		[self willChangeValueForKey:DCTNetworkActivityIndicatorControllerNetworkActivityKey];
  68
+		_networkActivity--;
  69
+		[self didChangeValueForKey:DCTNetworkActivityIndicatorControllerNetworkActivityKey];
78 70
 	
79  
-		[self dctInternal_calculateNetworkActivity];
80  
-	});
  71
+		[self _calculateNetworkActivity];
  72
+	}];
81 73
 }
82 74
 
83 75
 - (void)incrementNetworkActivity {
84  
-	dispatch_async(_queue, ^{
85  
-		[self willChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
86  
-		networkActivity++;
87  
-		[self didChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
  76
+	[_operationQueue addOperationWithBlock:^{
  77
+		[self willChangeValueForKey:DCTNetworkActivityIndicatorControllerNetworkActivityKey];
  78
+		_networkActivity++;
  79
+		[self didChangeValueForKey:DCTNetworkActivityIndicatorControllerNetworkActivityKey];
88 80
 	
89  
-		[self dctInternal_calculateNetworkActivity];
90  
-	});
  81
+		[self _calculateNetworkActivity];
  82
+	}];
91 83
 }
92 84
 
93  
-- (void)dctInternal_calculateNetworkActivity {
94  
-	dispatch_async(_queue, ^{
95  
-		[[NSNotificationCenter defaultCenter] postNotificationName:DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification object:self];
96  
-		[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(networkActivity > 0)];
97  
-	});
  85
+- (void)_calculateNetworkActivity {
  86
+	[[NSNotificationCenter defaultCenter] postNotificationName:DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification object:self];
  87
+	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(self.networkActivity > 0)];
98 88
 }
99 89
 
100 90
 - (NSString *)description {

0 notes on commit a6641e2

Please sign in to comment.
Something went wrong with that request. Please try again.