Skip to content

Commit

Permalink
[Issue #411] Dispatching upload/download progress blocks to the main …
Browse files Browse the repository at this point in the history
…queue
  • Loading branch information
mattt committed Aug 4, 2012
1 parent 84eae9d commit c9680e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 5 additions & 3 deletions AFNetworking/AFURLConnectionOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ extern NSString * const AFNetworkingOperationDidFinishNotification;
/**
Sets a callback to be called when an undetermined number of bytes have been uploaded to the server.
@param block A block object to be called when an undetermined number of bytes have been uploaded to the server. This block has no return value and takes three arguments: the number of bytes written since the last time the upload progress block was called, the total bytes written, and the total bytes expected to be written during the request, as initially determined by the length of the HTTP body. This block may be called multiple times.
@param block A block object to be called when an undetermined number of bytes have been uploaded to the server. This block has no return value and takes three arguments: the number of bytes written since the last time the upload progress block was called, the total bytes written, and the total bytes expected to be written during the request, as initially determined by the length of the HTTP body. This block may be called multiple times, and will execute on the main thread.
@discussion This block is called on the main thread.
@see setDownloadProgressBlock
*/
Expand All @@ -206,8 +208,8 @@ extern NSString * const AFNetworkingOperationDidFinishNotification;
/**
Sets a callback to be called when an undetermined number of bytes have been downloaded from the server.
@param block A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times.
@param block A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread.
@see setUploadProgressBlock
*/
- (void)setDownloadProgressBlock:(void (^)(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block;
Expand Down
8 changes: 6 additions & 2 deletions AFNetworking/AFURLConnectionOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,9 @@ - (void)connection:(NSURLConnection *)__unused connection
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
if (self.uploadProgress) {
self.uploadProgress(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
dispatch_async(dispatch_get_main_queue(), ^{
self.uploadProgress(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
});
}
}

Expand All @@ -562,7 +564,9 @@ - (void)connection:(NSURLConnection *)__unused connection
}

if (self.downloadProgress) {
self.downloadProgress((long long)[data length], self.totalBytesRead, self.response.expectedContentLength);
dispatch_async(dispatch_get_main_queue(), ^{
self.downloadProgress((long long)[data length], self.totalBytesRead, self.response.expectedContentLength);
});
}
}

Expand Down

0 comments on commit c9680e1

Please sign in to comment.