Skip to content

Commit

Permalink
Stop flickering in iOS network activity indicator by introducing a sm…
Browse files Browse the repository at this point in the history
…all delay before hiding it

Based on an idea from Jesse Rusak -> http://github.com/MindSea/asi-http-request/commit/f346f7f9de333501f732404323e8f73e911fbeea
  • Loading branch information
pokeb committed Aug 18, 2010
1 parent ce8d472 commit 5e4881a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
10 changes: 8 additions & 2 deletions Classes/ASIHTTPRequest.h
Expand Up @@ -725,9 +725,15 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
#pragma mark network activity

+ (BOOL)isNetworkInUse;
#if TARGET_OS_IPHONE

+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate;
#endif

// Shows the network activity spinner thing on iOS. You may wish to override this to do something else in Mac projects
+ (void)showNetworkActivityIndicator;

// Hides the network activity spinner thing on iOS
+ (void)hideNetworkActivityIndicator;


#pragma mark miscellany

Expand Down
45 changes: 31 additions & 14 deletions Classes/ASIHTTPRequest.m
Expand Up @@ -23,7 +23,7 @@


// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.7-42 2010-08-18";
NSString *ASIHTTPRequestVersion = @"v1.7-53 2010-08-18";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down Expand Up @@ -130,10 +130,12 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
// 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

// You can use [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO] if you want to manage it yourself
// Alternatively, override showNetworkActivityIndicator / hideNetworkActivityIndicator
// By default this does nothing on Mac OS X, but again override the above methods for a different behaviour
static BOOL shouldUpdateNetworkActivityIndicator = YES;
#endif


//**Queue stuff**/

Expand Down Expand Up @@ -2990,11 +2992,12 @@ - (void)destroyReadStream

if ([self readStreamIsScheduled]) {
runningRequestCount--;
#if TARGET_OS_IPHONE
if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
// Wait half a second before turning off the indicator
// This can prevent flicker when you have a single request finish and then immediately start another request
// We will cancel hiding the activity indicator if we start again
[[self class] performSelector:@selector(hideNetworkActivityIndicator) withObject:nil afterDelay:0.5];
}
#endif
}

[self setReadStreamIsScheduled:NO];
Expand All @@ -3014,11 +3017,10 @@ - (void)scheduleReadStream

[connectionsLock lock];
runningRequestCount++;
#if TARGET_OS_IPHONE
if (shouldUpdateNetworkActivityIndicator) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[NSObject cancelPreviousPerformRequestsWithTarget:[self class] selector:@selector(hideNetworkActivityIndicator) object:nil];
[[self class] showNetworkActivityIndicator];
}
#endif
[connectionsLock unlock];

// Reset the timeout
Expand All @@ -3028,17 +3030,19 @@ - (void)scheduleReadStream
}
}


- (void)unscheduleReadStream
{
if ([self readStream] && [self readStreamIsScheduled]) {

[connectionsLock lock];
runningRequestCount--;
#if TARGET_OS_IPHONE
if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
// Wait half a second before turning off the indicator
// This can prevent flicker when you have a single request finish and then immediately start another request
// We will cancel hiding the activity indicator if we start again
[[self class] performSelector:@selector(hideNetworkActivityIndicator) withObject:nil afterDelay:0.5];
}
#endif
[connectionsLock unlock];

[[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]];
Expand Down Expand Up @@ -3976,14 +3980,27 @@ + (BOOL)isNetworkInUse
[connectionsLock unlock];
return inUse;
}
#if TARGET_OS_IPHONE

+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate
{
[connectionsLock lock];
shouldUpdateNetworkActivityIndicator = shouldUpdate;
[connectionsLock unlock];
}

+ (void)showNetworkActivityIndicator
{
#if TARGET_OS_IPHONE
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
#endif
}

+ (void)hideNetworkActivityIndicator
{
#if TARGET_OS_IPHONE
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
#endif
}


#pragma mark threading behaviour
Expand Down

0 comments on commit 5e4881a

Please sign in to comment.