Merge pull request #988 from wantedly/improve-operation-behavior

Improve operation behavior
This commit is contained in:
Bogdan Poplauschi 2015-03-19 11:54:01 +02:00
commit beabb1842b
3 changed files with 28 additions and 7 deletions

View File

@ -10,9 +10,6 @@
#import "SDWebImageDownloaderOperation.h" #import "SDWebImageDownloaderOperation.h"
#import <ImageIO/ImageIO.h> #import <ImageIO/ImageIO.h>
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
static NSString *const kProgressCallbackKey = @"progress"; static NSString *const kProgressCallbackKey = @"progress";
static NSString *const kCompletedCallbackKey = @"completed"; static NSString *const kCompletedCallbackKey = @"completed";

View File

@ -10,6 +10,11 @@
#import "SDWebImageDownloader.h" #import "SDWebImageDownloader.h"
#import "SDWebImageOperation.h" #import "SDWebImageOperation.h"
extern NSString *const SDWebImageDownloadStartNotification;
extern NSString *const SDWebImageDownloadReceiveResponseNotification;
extern NSString *const SDWebImageDownloadStopNotification;
extern NSString *const SDWebImageDownloadFinishNotification;
@interface SDWebImageDownloaderOperation : NSOperation <SDWebImageOperation> @interface SDWebImageDownloaderOperation : NSOperation <SDWebImageOperation>
/** /**
@ -39,6 +44,16 @@
*/ */
@property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options; @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 * Initializes a `SDWebImageDownloaderOperation` object
* *

View File

@ -12,6 +12,11 @@
#import <ImageIO/ImageIO.h> #import <ImageIO/ImageIO.h>
#import "SDWebImageManager.h" #import "SDWebImageManager.h"
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadReceiveResponseNotification = @"SDWebImageDownloadReceiveResponseNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
NSString *const SDWebImageDownloadFinishNotification = @"SDWebImageDownloadFinishNotification";
@interface SDWebImageDownloaderOperation () <NSURLConnectionDataDelegate> @interface SDWebImageDownloaderOperation () <NSURLConnectionDataDelegate>
@property (copy, nonatomic) SDWebImageDownloaderProgressBlock progressBlock; @property (copy, nonatomic) SDWebImageDownloaderProgressBlock progressBlock;
@ -20,7 +25,6 @@
@property (assign, nonatomic, getter = isExecuting) BOOL executing; @property (assign, nonatomic, getter = isExecuting) BOOL executing;
@property (assign, nonatomic, getter = isFinished) BOOL finished; @property (assign, nonatomic, getter = isFinished) BOOL finished;
@property (assign, nonatomic) NSInteger expectedSize;
@property (strong, nonatomic) NSMutableData *imageData; @property (strong, nonatomic) NSMutableData *imageData;
@property (strong, nonatomic) NSURLConnection *connection; @property (strong, nonatomic) NSURLConnection *connection;
@property (strong, atomic) NSThread *thread; @property (strong, atomic) NSThread *thread;
@ -210,6 +214,10 @@
} }
self.imageData = [[NSMutableData alloc] initWithCapacity:expected]; self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
self.response = response;
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:self];
});
} }
else { else {
NSUInteger code = [((NSHTTPURLResponse *)response) statusCode]; NSUInteger code = [((NSHTTPURLResponse *)response) statusCode];
@ -222,7 +230,7 @@
[self.connection cancel]; [self.connection cancel];
} }
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
}); });
if (self.completedBlock) { if (self.completedBlock) {
@ -352,7 +360,8 @@
self.thread = nil; self.thread = nil;
self.connection = nil; self.connection = nil;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
}); });
} }
@ -393,7 +402,7 @@
self.thread = nil; self.thread = nil;
self.connection = nil; self.connection = nil;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
}); });
} }