Adding progress callback to AFHTTPRequestOperations, which can be used to monitor the progress of POST data being sent in a file upload, for instance

This commit is contained in:
Mattt Thompson 2011-08-14 19:53:48 -05:00
parent b734a64add
commit a2138c6bb0
2 changed files with 20 additions and 0 deletions

View file

@ -52,4 +52,6 @@ extern NSString * const AFHTTPOperationDidFinishNotification;
- (id)initWithRequest:(NSURLRequest *)urlRequest;
- (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block;
@end

View file

@ -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;