Adding progress block to AFHTTPRequestOperation
Moving AFHTTPRequestOperation -initWithURLRequest: to class extension
This commit is contained in:
parent
8833578a14
commit
bc4b92335e
3 changed files with 32 additions and 14 deletions
|
|
@ -51,6 +51,6 @@ extern NSString * const AFHTTPOperationDidFinishNotification;
|
||||||
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
||||||
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion;
|
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion;
|
||||||
|
|
||||||
- (id)initWithRequest:(NSURLRequest *)urlRequest;
|
- (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ typedef enum {
|
||||||
NSString * const AFHTTPOperationDidStartNotification = @"com.alamofire.http-operation.start";
|
NSString * const AFHTTPOperationDidStartNotification = @"com.alamofire.http-operation.start";
|
||||||
NSString * const AFHTTPOperationDidFinishNotification = @"com.alamofire.http-operation.finish";
|
NSString * const AFHTTPOperationDidFinishNotification = @"com.alamofire.http-operation.finish";
|
||||||
|
|
||||||
|
typedef void (^AFHTTPRequestOperationProgressBlock)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite);
|
||||||
typedef void (^AFHTTPRequestOperationCompletionBlock)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error);
|
typedef void (^AFHTTPRequestOperationCompletionBlock)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error);
|
||||||
|
|
||||||
static inline NSString * AFKeyPathFromOperationState(AFHTTPOperationState state) {
|
static inline NSString * AFKeyPathFromOperationState(AFHTTPOperationState state) {
|
||||||
|
|
@ -80,8 +81,10 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
|
||||||
@property (nonatomic, assign) BOOL isCancelled;
|
@property (nonatomic, assign) BOOL isCancelled;
|
||||||
@property (readwrite, nonatomic, retain) NSPort *port;
|
@property (readwrite, nonatomic, retain) NSPort *port;
|
||||||
@property (readwrite, nonatomic, retain) NSMutableData *dataAccumulator;
|
@property (readwrite, nonatomic, retain) NSMutableData *dataAccumulator;
|
||||||
|
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationProgressBlock progress;
|
||||||
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion;
|
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion;
|
||||||
|
|
||||||
|
- (id)initWithRequest:(NSURLRequest *)urlRequest;
|
||||||
- (void)cleanup;
|
- (void)cleanup;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
@ -96,6 +99,7 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
|
||||||
@synthesize error = _error;
|
@synthesize error = _error;
|
||||||
@synthesize responseBody = _responseBody;
|
@synthesize responseBody = _responseBody;
|
||||||
@synthesize dataAccumulator = _dataAccumulator;
|
@synthesize dataAccumulator = _dataAccumulator;
|
||||||
|
@synthesize progress = _progress;
|
||||||
@synthesize completion = _completion;
|
@synthesize completion = _completion;
|
||||||
|
|
||||||
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
||||||
|
|
@ -134,9 +138,14 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
|
||||||
[_connection release];
|
[_connection release];
|
||||||
|
|
||||||
[_completion release];
|
[_completion release];
|
||||||
|
[_progress release];
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block {
|
||||||
|
self.progress = block;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)cleanup {
|
- (void)cleanup {
|
||||||
for (NSString *runLoopMode in self.runLoopModes) {
|
for (NSString *runLoopMode in self.runLoopModes) {
|
||||||
[[NSRunLoop currentRunLoop] removePort:self.port forMode:runLoopMode];
|
[[NSRunLoop currentRunLoop] removePort:self.port forMode:runLoopMode];
|
||||||
|
|
@ -239,14 +248,18 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
|
||||||
|
|
||||||
#pragma mark - NSURLConnection
|
#pragma mark - NSURLConnection
|
||||||
|
|
||||||
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
- (void)connection:(NSURLConnection *)connection
|
||||||
|
didReceiveResponse:(NSURLResponse *)response
|
||||||
|
{
|
||||||
self.response = (NSHTTPURLResponse *)response;
|
self.response = (NSHTTPURLResponse *)response;
|
||||||
NSUInteger contentLength = MIN(MAX(abs(response.expectedContentLength), 1024), 1024 * 1024 * 8);
|
NSUInteger contentLength = MIN(MAX(abs(response.expectedContentLength), 1024), 1024 * 1024 * 8);
|
||||||
|
|
||||||
self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength];
|
self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
|
- (void)connection:(NSURLConnection *)connection
|
||||||
|
didReceiveData:(NSData *)data
|
||||||
|
{
|
||||||
[self.dataAccumulator appendData:data];
|
[self.dataAccumulator appendData:data];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,7 +272,9 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
|
||||||
[self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
|
[self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
- (void)connection:(NSURLConnection *)connection
|
||||||
|
didFailWithError:(NSError *)error
|
||||||
|
{
|
||||||
self.state = AFHTTPOperationFinishedState;
|
self.state = AFHTTPOperationFinishedState;
|
||||||
|
|
||||||
self.error = error;
|
self.error = error;
|
||||||
|
|
@ -267,6 +282,16 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
|
||||||
[self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
|
[self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)connection:(NSURLConnection *)connection
|
||||||
|
didSendBodyData:(NSInteger)bytesWritten
|
||||||
|
totalBytesWritten:(NSInteger)totalBytesWritten
|
||||||
|
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
|
||||||
|
{
|
||||||
|
if (self.progress) {
|
||||||
|
self.progress(totalBytesWritten, totalBytesExpectedToWrite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
|
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
|
||||||
if ([self isCancelled]) {
|
if ([self isCancelled]) {
|
||||||
return nil;
|
return nil;
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
|
||||||
options:(AFImageRequestOptions)options
|
options:(AFImageRequestOptions)options
|
||||||
success:(void (^)(UIImage *image))success
|
success:(void (^)(UIImage *image))success
|
||||||
{
|
{
|
||||||
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
AFImageRequestOperation *operation = [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
||||||
UIImage *image = nil;
|
UIImage *image = nil;
|
||||||
if ([[UIScreen mainScreen] scale] == 2.0) {
|
if ([[UIScreen mainScreen] scale] == 2.0) {
|
||||||
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
|
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
|
||||||
|
|
@ -70,17 +70,10 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
|
||||||
[[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];
|
[[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];
|
||||||
});
|
});
|
||||||
}];
|
}];
|
||||||
}
|
|
||||||
|
|
||||||
- (id)initWithRequest:(NSURLRequest *)urlRequest {
|
operation.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];
|
||||||
self = [super initWithRequest:urlRequest];
|
|
||||||
if (!self) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];
|
return operation;
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue