Skip to content

Commit

Permalink
Merge pull request SDWebImage#988 from wantedly/improve-operation-beh…
Browse files Browse the repository at this point in the history
…avior

Improve operation behavior
  • Loading branch information
bpoplauschi committed Mar 19, 2015
2 parents 0ef0e07 + ce73619 commit beabb18
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 0 additions & 3 deletions SDWebImage/SDWebImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#import "SDWebImageDownloaderOperation.h"
#import <ImageIO/ImageIO.h>

NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";

static NSString *const kProgressCallbackKey = @"progress";
static NSString *const kCompletedCallbackKey = @"completed";

Expand Down
15 changes: 15 additions & 0 deletions SDWebImage/SDWebImageDownloaderOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#import "SDWebImageDownloader.h"
#import "SDWebImageOperation.h"

extern NSString *const SDWebImageDownloadStartNotification;
extern NSString *const SDWebImageDownloadReceiveResponseNotification;
extern NSString *const SDWebImageDownloadStopNotification;
extern NSString *const SDWebImageDownloadFinishNotification;

@interface SDWebImageDownloaderOperation : NSOperation <SDWebImageOperation>

/**
Expand Down Expand Up @@ -39,6 +44,16 @@
*/
@property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;

/**
* The expected size of data.
*/
@property (assign, nonatomic) NSInteger expectedSize;

/**
* The response returned by the operation's connection.
*/
@property (strong, nonatomic) NSURLResponse *response;

/**
* Initializes a `SDWebImageDownloaderOperation` object
*
Expand Down
17 changes: 13 additions & 4 deletions SDWebImage/SDWebImageDownloaderOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#import <ImageIO/ImageIO.h>
#import "SDWebImageManager.h"

NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadReceiveResponseNotification = @"SDWebImageDownloadReceiveResponseNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
NSString *const SDWebImageDownloadFinishNotification = @"SDWebImageDownloadFinishNotification";

@interface SDWebImageDownloaderOperation () <NSURLConnectionDataDelegate>

@property (copy, nonatomic) SDWebImageDownloaderProgressBlock progressBlock;
Expand All @@ -20,7 +25,6 @@ @interface SDWebImageDownloaderOperation () <NSURLConnectionDataDelegate>

@property (assign, nonatomic, getter = isExecuting) BOOL executing;
@property (assign, nonatomic, getter = isFinished) BOOL finished;
@property (assign, nonatomic) NSInteger expectedSize;
@property (strong, nonatomic) NSMutableData *imageData;
@property (strong, nonatomic) NSURLConnection *connection;
@property (strong, atomic) NSThread *thread;
Expand Down Expand Up @@ -210,6 +214,10 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
}

self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
self.response = response;
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:self];
});
}
else {
NSUInteger code = [((NSHTTPURLResponse *)response) statusCode];
Expand All @@ -222,7 +230,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
[self.connection cancel];
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
});

if (self.completedBlock) {
Expand Down Expand Up @@ -352,7 +360,8 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
self.thread = nil;
self.connection = nil;
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
});
}

Expand Down Expand Up @@ -393,7 +402,7 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
self.thread = nil;
self.connection = nil;
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
});
}

Expand Down

0 comments on commit beabb18

Please sign in to comment.