Permalink
Browse files

Adding support for download progress block when an output block is used

  • Loading branch information...
1 parent 59a98bb commit 8de22e8f99ea788b836e7b2aa05ea6bcc37f51c1 @mattt mattt committed Aug 24, 2011
Showing with 10 additions and 4 deletions.
  1. +10 −4 AFNetworking/AFHTTPRequestOperation.m
@@ -79,6 +79,7 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
@interface AFHTTPRequestOperation ()
@property (nonatomic, assign) AFHTTPOperationState state;
@property (nonatomic, assign) BOOL isCancelled;
+@property (readwrite, nonatomic, assign) NSUInteger totalBytesRead;
@property (readwrite, nonatomic, retain) NSMutableData *dataAccumulator;
@property (readwrite, nonatomic, retain) NSOutputStream *outputStream;
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationProgressBlock uploadProgress;
@@ -97,6 +98,7 @@ @implementation AFHTTPRequestOperation
@synthesize response = _response;
@synthesize error = _error;
@synthesize responseBody = _responseBody;
+@synthesize totalBytesRead = _totalBytesRead;
@synthesize dataAccumulator = _dataAccumulator;
@synthesize outputStream = _outputStream;
@synthesize uploadProgress = _uploadProgress;
@@ -301,17 +303,19 @@ - (void)connection:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
+ self.totalBytesRead += [data length];
+
if (self.outputStream) {
if ([self.outputStream hasSpaceAvailable]) {
const uint8_t *dataBuffer = [data bytes];
[self.outputStream write:&dataBuffer[0] maxLength:[data length]];
}
} else {
[self.dataAccumulator appendData:data];
+ }
- if (self.downloadProgress) {
- self.downloadProgress([self.dataAccumulator length], self.response.expectedContentLength);
- }
+ if (self.downloadProgress) {
+ self.downloadProgress(self.totalBytesRead, self.response.expectedContentLength);
}
}
@@ -348,7 +352,9 @@ - (void)connection:(NSURLConnection *)connection
}
}
-- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
+- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
+ willCacheResponse:(NSCachedURLResponse *)cachedResponse
+{
if ([self isCancelled]) {
return nil;
}

0 comments on commit 8de22e8

Please sign in to comment.