diff --git a/AFNetworking/AFHTTPRequestOperation.h b/AFNetworking/AFHTTPRequestOperation.h index 2f2a503..c69a657 100644 --- a/AFNetworking/AFHTTPRequestOperation.h +++ b/AFNetworking/AFHTTPRequestOperation.h @@ -52,4 +52,6 @@ extern NSString * const AFHTTPOperationDidFinishNotification; - (id)initWithRequest:(NSURLRequest *)urlRequest; +- (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block; + @end diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 9ce36da..d932f7c 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -33,6 +33,7 @@ typedef enum { NSString * const AFHTTPOperationDidStartNotification = @"com.alamofire.http-operation.start"; 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); static inline NSString * AFKeyPathFromOperationState(AFHTTPOperationState state) { @@ -79,6 +80,7 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr @property (nonatomic, assign) AFHTTPOperationState state; @property (nonatomic, assign) BOOL isCancelled; @property (readwrite, nonatomic, retain) NSMutableData *dataAccumulator; +@property (readwrite, nonatomic, copy) AFHTTPRequestOperationProgressBlock progress; @property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion; - (void)cleanup; @@ -94,6 +96,7 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr @synthesize error = _error; @synthesize responseBody = _responseBody; @synthesize dataAccumulator = _dataAccumulator; +@synthesize progress = _progress; @synthesize completion = _completion; + (id)operationWithRequest:(NSURLRequest *)urlRequest @@ -130,6 +133,7 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr [_connection release]; + [_progress release]; [_completion release]; [super dealloc]; } @@ -141,6 +145,10 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]); } +- (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block { + self.progress = block; +} + - (void)setState:(AFHTTPOperationState)state { if (!AFHTTPOperationStateTransitionIsValid(self.state, state)) { return; @@ -261,6 +269,16 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr [self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:NO]; } +- (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 { if ([self isCancelled]) { return nil;