Ben Copsey

Move network activity indicator updates out of ASINetworkQueue and into ASIHTTPRequest

Add new isNetworkInUse class method that works on Mac too
... ... @@ -702,6 +702,13 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
// Returns the maximum amount of data we can read as part of the current measurement period, and sleeps this thread if our allowance is used up
+ (unsigned long)maxUploadReadLength;
#pragma mark network activity
+ (BOOL)isNetworkInUse;
#if TARGET_OS_IPHONE
+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate;
#endif
#pragma mark miscellany
// Determines whether we're on iPhone OS 2.0 at runtime, currently used to determine whether we should apply a workaround for an issue with converting longs to doubles on iPhone OS 2
... ...
... ... @@ -24,7 +24,7 @@
// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.6.2-58 2010-06-23";
NSString *ASIHTTPRequestVersion = @"v1.6.2-61 2010-06-23";
NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
... ... @@ -124,6 +124,15 @@ static BOOL isiPhoneOS2;
static id <ASICacheDelegate> defaultCache = nil;
// Used for tracking when requests are using the network
static unsigned int runningRequestCount = 0;
#if TARGET_OS_IPHONE
// Use [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO] if you want to manage it yourself
static BOOL shouldUpdateNetworkActivityIndicator = YES;
#endif
//**Queue stuff**/
// The thread all requests will run on
... ... @@ -2825,12 +2834,22 @@ static NSOperationQueue *sharedQueue = nil;
{
if ([self readStream]) {
CFReadStreamSetClient((CFReadStreamRef)[self readStream], kCFStreamEventNone, NULL, NULL);
[connectionsLock lock];
[connectionsLock lock];
if (shouldUpdateNetworkActivityIndicator && [self readStreamIsScheduled]) {
runningRequestCount--;
#if TARGET_OS_IPHONE
if (runningRequestCount == 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
#endif
}
[self setReadStreamIsScheduled:NO];
if (![self connectionCanBeReused]) {
[[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]];
[[self readStream] close];
[self setReadStreamIsScheduled:NO];
}
[self setReadStream:nil];
[connectionsLock unlock];
... ... @@ -2840,6 +2859,16 @@ static NSOperationQueue *sharedQueue = nil;
- (void)scheduleReadStream
{
if ([self readStream] && ![self readStreamIsScheduled]) {
[connectionsLock lock];
runningRequestCount++;
#if TARGET_OS_IPHONE
if (shouldUpdateNetworkActivityIndicator) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
#endif
[connectionsLock unlock];
// Reset the timeout
[self setLastActivityTime:[NSDate date]];
[[self readStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]];
... ... @@ -2850,6 +2879,16 @@ static NSOperationQueue *sharedQueue = nil;
- (void)unscheduleReadStream
{
if ([self readStream] && [self readStreamIsScheduled]) {
[connectionsLock lock];
runningRequestCount--;
#if TARGET_OS_IPHONE
if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
#endif
[connectionsLock unlock];
[[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]];
[self setReadStreamIsScheduled:NO];
}
... ... @@ -3757,6 +3796,25 @@ static NSOperationQueue *sharedQueue = nil;
}
#endif
#pragma mark network activity
+ (BOOL)isNetworkInUse
{
[connectionsLock lock];
BOOL inUse = (runningRequestCount > 0);
[connectionsLock unlock];
return inUse;
}
#if TARGET_OS_IPHONE
+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate
{
[connectionsLock lock];
shouldUpdateNetworkActivityIndicator = shouldUpdate;
[connectionsLock unlock];
}
#endif
#pragma mark threading behaviour
// In the default implementation, all requests run in a single background thread
... ...
... ... @@ -80,14 +80,6 @@
// This method will start the queue
- (void)go;
// Used on iPhone platform to show / hide the network activity indicator (in the status bar)
// On mac, you could subclass to do something else
- (void)updateNetworkActivityIndicator;
// Returns YES if the queue is in progress
- (BOOL)isNetworkActive;
@property (assign,setter=setUploadProgressDelegate:) id uploadProgressDelegate;
@property (assign,setter=setDownloadProgressDelegate:) id downloadProgressDelegate;
... ...
... ... @@ -42,22 +42,9 @@
[super dealloc];
}
- (BOOL)isNetworkActive
{
return ([self requestsCount] > 0 && ![self isSuspended]);
}
- (void)updateNetworkActivityIndicator
{
#if TARGET_OS_IPHONE
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActive]];
#endif
}
- (void)setSuspended:(BOOL)suspend
{
[super setSuspended:suspend];
[self updateNetworkActivityIndicator];
}
- (void)reset
... ... @@ -87,7 +74,6 @@
[self setBytesDownloadedSoFar:0];
[self setTotalBytesToDownload:0];
[super cancelAllOperations];
[self updateNetworkActivityIndicator];
}
- (void)setUploadProgressDelegate:(id)newDelegate
... ... @@ -190,7 +176,6 @@
[request setQueue:self];
[super addOperation:request];
[self updateNetworkActivityIndicator];
}
... ... @@ -212,7 +197,6 @@
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self setRequestsCount:[self requestsCount]-1];
[self updateNetworkActivityIndicator];
if ([self requestDidFinishSelector]) {
[[self delegate] performSelector:[self requestDidFinishSelector] withObject:request];
}
... ... @@ -226,7 +210,6 @@
- (void)requestFailed:(ASIHTTPRequest *)request
{
[self setRequestsCount:[self requestsCount]-1];
[self updateNetworkActivityIndicator];
if ([self requestDidFailSelector]) {
[[self delegate] performSelector:[self requestDidFailSelector] withObject:request];
}
... ...
... ... @@ -46,7 +46,6 @@ Most of the code below here relates to the table view, and isn't that interestin
}
- (void)dealloc
{
[request cancel];
... ...