Add support for system network activity indicator show/hide via notification

As showing/hiding the network activity indicator could conflict with your own code (i.e. the lib may hide the indicator when your code would still do some network stuff), the show/hide of the indicator isn't performed by the lib directly but SDWebImageDownloadStartNotification and SDWebImageDownloadStopNotification notifications are posted instead.

This lets you handle the indicator visiblity by yourself. If you're lazy, you can use the SDNetworkActivityIndicator library (http://github.com/rs/SDNetworkActivityIndicator) to handle it automatically. Once added to your project, all you have to do is to import this lib in addition to the SDWebImage lib.

Note that you should then use SDNetworkActivityIndicator for all your network status indicator visibility changes in your code if you don't want conflicts to happen.
This commit is contained in:
Olivier Poitrey 2010-10-03 10:04:41 +02:00
parent 2b352c3c3a
commit 3f2f360ee2
2 changed files with 27 additions and 0 deletions

View File

@ -9,6 +9,9 @@
#import <Foundation/Foundation.h>
#import "SDWebImageDownloaderDelegate.h"
extern NSString *const SDWebImageDownloadStartNotification;
extern NSString *const SDWebImageDownloadStopNotification;
@interface SDWebImageDownloader : NSObject
{
@private

View File

@ -8,6 +8,9 @@
#import "SDWebImageDownloader.h"
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
@interface SDWebImageDownloader ()
@property (nonatomic, retain) NSURLConnection *connection;
@end
@ -19,6 +22,19 @@
+ (id)downloaderWithURL:(NSURL *)url delegate:(id<SDWebImageDownloaderDelegate>)delegate
{
// Bind SDNetworkActivityIndicator if available (download it here: http://github.com/rs/SDNetworkActivityIndicator )
// To use it, just add #import "SDNetworkActivityIndicator.h" in addition to the SDWebImage import
if (NSClassFromString(@"SDNetworkActivityIndicator"))
{
id activityIndicator = [NSClassFromString(@"SDNetworkActivityIndicator") performSelector:NSSelectorFromString(@"sharedActivityIndicator")];
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
selector:NSSelectorFromString(@"startActivity")
name:SDWebImageDownloadStartNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
selector:NSSelectorFromString(@"stopActivity")
name:SDWebImageDownloadStopNotification object:nil];
}
SDWebImageDownloader *downloader = [[[SDWebImageDownloader alloc] init] autorelease];
downloader.url = url;
downloader.delegate = delegate;
@ -44,6 +60,7 @@
if (connection)
{
self.imageData = [NSMutableData data];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:nil];
}
else
{
@ -60,6 +77,7 @@
{
[connection cancel];
self.connection = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
}
}
@ -70,10 +88,13 @@
[imageData appendData:data];
}
#pragma GCC diagnostic ignored "-Wundeclared-selector"
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
{
self.connection = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
if ([delegate respondsToSelector:@selector(imageDownloaderDidFinish:)])
{
[delegate performSelector:@selector(imageDownloaderDidFinish:) withObject:self];
@ -89,6 +110,8 @@
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)])
{
[delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:error];
@ -102,6 +125,7 @@
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[url release], url = nil;
[connection release], connection = nil;
[imageData release], imageData = nil;