Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: AFNetworking/AFNetworking
...
head fork: mpurland/AFNetworking
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 28, 2014
@mpurland mpurland Allow AFURLSessionManager dataTaskWithRequest to include data handler…
… block that corresponds to NSURLSessionDataTaskDelegate URLSession:dataTask:didReceiveData: for that data task. This is useful in creating generic handlers for chunked responses and progress.
f725cc9
View
11 AFNetworking/AFURLSessionManager.h
@@ -205,6 +205,17 @@
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;
+/**
+ Creates an `NSURLSessionDataTask` with the specified request.
+
+ @param request The HTTP request for the request.
+ @param completionHandler A block object to be executed when the task finishes. This block has no return value and takes three arguments: the server response, the response object created by that serializer, and the error that occurred, if any.
+ @param dataHandler A block object to be executed when data is received. This block has no return value and takes three arguments: the url session, the task itself, and the data object.
+ */
+- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
+ completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
+ dataHandler:(void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data))dataHandler;
+
///---------------------------
/// @name Running Upload Tasks
///---------------------------
View
22 AFNetworking/AFURLSessionManager.m
@@ -102,6 +102,7 @@ static dispatch_group_t url_session_manager_completion_group() {
typedef void (^AFURLSessionDownloadTaskDidResumeBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t fileOffset, int64_t expectedTotalBytes);
typedef void (^AFURLSessionTaskCompletionHandler)(NSURLResponse *response, id responseObject, NSError *error);
+typedef void (^AFURLSessionTaskDataHandler)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data);
#pragma mark -
@@ -112,6 +113,7 @@ @interface AFURLSessionManagerTaskDelegate : NSObject <NSURLSessionTaskDelegate,
@property (nonatomic, copy) NSURL *downloadFileURL;
@property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;
@property (nonatomic, copy) AFURLSessionTaskCompletionHandler completionHandler;
+@property (nonatomic, copy) AFURLSessionTaskDataHandler dataHandler;
@end
@implementation AFURLSessionManagerTaskDelegate
@@ -210,6 +212,9 @@ - (void)URLSession:(__unused NSURLSession *)session
didReceiveData:(NSData *)data
{
[self.mutableData appendData:data];
+ if (self.dataHandler) {
+ self.dataHandler(session, dataTask, data);
+ }
}
#pragma mark - NSURLSessionDownloadTaskDelegate
@@ -314,7 +319,7 @@ - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)config
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDataTask *task in dataTasks) {
- [self addDelegateForDataTask:task completionHandler:nil];
+ [self addDelegateForDataTask:task completionHandler:nil dataHandler:nil];
}
for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
@@ -356,10 +361,12 @@ - (void)setDelegate:(AFURLSessionManagerTaskDelegate *)delegate
- (void)addDelegateForDataTask:(NSURLSessionDataTask *)dataTask
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
+ dataHandler:(void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data))dataHandler
{
AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init];
delegate.manager = self;
delegate.completionHandler = completionHandler;
+ delegate.dataHandler = dataHandler;
[self setDelegate:delegate forTask:dataTask];
}
@@ -497,13 +504,20 @@ - (void)setResponseSerializer:(id <AFURLResponseSerialization>)responseSerialize
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
+ return [self dataTaskWithRequest:request completionHandler:completionHandler dataHandler:nil];
+}
+
+- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
+ completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
+ dataHandler:(void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data))dataHandler
+{
__block NSURLSessionDataTask *dataTask = nil;
dispatch_sync(url_session_manager_creation_queue(), ^{
dataTask = [self.session dataTaskWithRequest:request];
});
-
- [self addDelegateForDataTask:dataTask completionHandler:completionHandler];
-
+
+ [self addDelegateForDataTask:dataTask completionHandler:completionHandler dataHandler:dataHandler];
+
return dataTask;
}

No commit comments for this range

Something went wrong with that request. Please try again.