0

I am using NSURLSessionUploadTask to upload an image.

The upload works. The image is successfully posted to my server.

This is the method that is called creates a new NSURLSessionUploadTask. The NSData is the image in NSData format, and the withPostRequest: is a NSMutableRequest with all the junk that goes in that.

- (void) uploadImage:(NSData*)data withPostRequest:(NSMutableURLRequest*)postRequest {

    NSURLSessionUploadTask * uploadTask = [_session uploadTaskWithRequest:postRequest fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {

            //MAIN THREAD ACCESS UPDATE TO THE GUI OR DISPLAY ERRORS
            dispatch_async(dispatch_get_main_queue(), ^{

               //UPDATE THE GUI ON COMPLETION
            });

        }];

        // START THE TASK
        [uploadTask resume];
}

This delegate method gets called with the upload to show the progress. This method IS called upon ever successful byte sent.

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {

NSData * taskData = [task.originalRequest HTTPBody];
            NSLog(@"Task Data: %@", taskData);

}

However, if I have multiple uploads, I need to be able to check which upload has the correct progress. I feel like you can do this by comparing the task.originalRequest NSData, with the known NSData of the passed Image.

However, when the above method is called, [task.originalRequest HTTPBody] is NULL

Does anyone know why that would be null?

user-44651
  • 3,924
  • 6
  • 41
  • 87

1 Answers1

0

You can subclass the NSSessionUploadTask and have an identifier assocciated with it.

@interface NSSessionUploadTaskId : NSObject {
   NSUInteger taskIdentifier;
}

NSURLSessionTask also has taskIdentifier property but I'm not sure how to use that.

@property(readonly) NSUInteger taskIdentifier;

An identifier uniquely identifies the task within a given session.

JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51