Skip to content

Commit

Permalink
setNetworkActivityIndicator is now set based on the number of ongoing…
Browse files Browse the repository at this point in the history
… *active* requests, not the operation queue's count.
  • Loading branch information
samvermette committed Aug 6, 2012
1 parent 15c239c commit 67a98b2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions SVHTTPRequest/SVHTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@

@property (nonatomic, readwrite) BOOL sendParametersAsJSON;
@property (nonatomic, readwrite) NSURLRequestCachePolicy cachePolicy;
@property (nonatomic, readwrite) NSUInteger activeRequestCount;

@end
21 changes: 11 additions & 10 deletions SVHTTPRequest/SVHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (void)queueRequest:(NSString*)path

@implementation SVHTTPClient

@synthesize username, password, basePath, userAgent, sendParametersAsJSON, cachePolicy, operationQueue, HTTPHeaderFields;
@synthesize username, password, basePath, userAgent, sendParametersAsJSON, cachePolicy, operationQueue, HTTPHeaderFields, activeRequestCount;


+ (id)sharedClient {
Expand All @@ -47,20 +47,20 @@ - (id)init {
if (self = [super init]) {
self.operationQueue = [[NSOperationQueue alloc] init];

[self.operationQueue addObserver:self
forKeyPath:@"operationCount"
options:NSKeyValueObservingOptionNew
context:&self->operationQueue];
[self addObserver:self
forKeyPath:@"activeRequestCount"
options:NSKeyValueObservingOptionNew
context:nil];
}

return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"operationCount"]) {
if ([keyPath isEqualToString:@"activeRequestCount"]) {
#if TARGET_OS_IPHONE
dispatch_async(dispatch_get_main_queue(), ^{
BOOL indicatorVisible = self.operationQueue.operationCount > 0;
BOOL indicatorVisible = activeRequestCount > 0;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:indicatorVisible];
});
#endif
Expand All @@ -69,9 +69,8 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

- (void)dealloc
{
[self.operationQueue removeObserver:self forKeyPath:@"operationCount" context:&self->operationQueue];
- (void)dealloc {
[self removeObserver:self forKeyPath:@"activeRequestCount" context:nil];
}

#pragma mark - Setters
Expand Down Expand Up @@ -157,6 +156,8 @@ - (void)queueRequest:(NSString*)path
requestOperation.cachePolicy = self.cachePolicy;
requestOperation.userAgent = self.userAgent;

[(id<SVHTTPRequestPrivateMethods>)requestOperation setClient:self];

[self.HTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *field, NSString *value, BOOL *stop) {
[(id<SVHTTPRequestPrivateMethods>)requestOperation setValue:value forHTTPHeaderField:field];
}];
Expand Down
2 changes: 1 addition & 1 deletion SVHTTPRequest/SVHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef NSUInteger SVHTTPRequestMethod;
@protocol SVHTTPRequestPrivateMethods <NSObject>

@property (nonatomic, strong) NSString *requestPath;
@property (nonatomic, strong) SVHTTPClient *client;

- (SVHTTPRequest*)initWithAddress:(NSString*)urlString
method:(SVHTTPRequestMethod)method
Expand All @@ -60,7 +61,6 @@ typedef NSUInteger SVHTTPRequestMethod;
completion:(void (^)(id, NSHTTPURLResponse*, NSError*))completionBlock;

- (void)signRequestWithUsername:(NSString*)username password:(NSString*)password;

- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

@end
7 changes: 5 additions & 2 deletions SVHTTPRequest/SVHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ @interface SVHTTPRequest ()

@property (nonatomic, readwrite) SVHTTPRequestState state;
@property (nonatomic, strong) NSString *requestPath;
@property (nonatomic, strong) SVHTTPClient *client;

@property (nonatomic, strong) NSTimer *timeoutTimer; // see http://stackoverflow.com/questions/2736967

Expand All @@ -68,7 +69,7 @@ @implementation SVHTTPRequest
@synthesize operationRequest, operationData, operationConnection, operationParameters, operationURLResponse, operationFileHandle, state;
@synthesize operationSavePath, operationCompletionBlock, operationProgressBlock, timeoutTimer;
@synthesize expectedContentLength, receivedContentLength, saveDataDispatchGroup, saveDataDispatchQueue;
@synthesize requestPath, userAgent;
@synthesize requestPath, userAgent, client;

- (void)dealloc {
[operationConnection cancel];
Expand Down Expand Up @@ -301,6 +302,7 @@ - (void)start {
[self.operationConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

[self.operationConnection start];
self.client.activeRequestCount++;

#if !(defined SVHTTPREQUEST_DISABLE_LOGGING)
NSLog(@"[%@] %@", self.operationRequest.HTTPMethod, self.operationRequest.URL.absoluteString);
Expand Down Expand Up @@ -425,7 +427,8 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err

- (void)callCompletionBlockWithResponse:(id)response error:(NSError *)error {
self.timeoutTimer = nil;

self.client.activeRequestCount--;

dispatch_async(dispatch_get_main_queue(), ^{
NSError *serverError = error;

Expand Down

0 comments on commit 67a98b2

Please sign in to comment.