1

I have a background upload task that is defined in the following manner:

NSURLSessionUploadTask* task = [session_ uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:httpBody]];

I would like to deletehttpBody once the upload task is finished, which happens in my delegate in this function:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    Q_UNUSED(session);
    if (error)
    {
        reportError(callback, error);
    }
    else
    {
        NSMutableData *responseData = self.responsesData[@(task.taskIdentifier)];
        if (responseData)
        {
            NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
            if (response)
            {
                LOG_INFO << [NSString stringWithFormat:@"%@", response];
            } 
            else
            {
                LOG_INFO << [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
            }
        [self.responsesData removeObjectForKey:@(task.taskIdentifier)];
        }
        (*callback)(boost::none);
    }
}

I am not sure how to access httpBody in didCompleteWithError so I can delete it. How do I do this?

siavashk
  • 436
  • 7
  • 24

1 Answers1

0

You can get back the originalRequest from the task. If there is nothing in there to use, you can create the request as an NSMutableRequest to start and use:

+ (void)setProperty:(id)value 
         forKey:(NSString *)key 
      inRequest:(NSMutableURLRequest *)request;

To add your filename to it. You can get this later with:

+ (id)propertyForKey:(NSString *)key 
       inRequest:(NSURLRequest *)request; 

More info:

https://developer.apple.com/documentation/foundation/nsurlprotocol/1407897-setproperty?language=objc

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Thanks. I am going to try it now and will let you know if it works out. I come from a C++ background and I am kind of new to objective-c(++). – siavashk Oct 25 '17 at 20:14
  • Is there a special reason not to add the filename as part of the http header? Something like this for storing the filename: `[request setValue:httpBody forHTTPHeaderField:@"httpBodyFile"]` And something like this for retrieving: `[task.originalRequest valueForHTTPHeaderField:@"httpBodyFile”]` The reason is that I do not know how to properly subclass from `NSURLProtocol` or `NSMutableURLRequest` or whatever the appropriate class is. – siavashk Oct 25 '17 at 21:35
  • 1
    Your made up headers should start with x-appname- to not interfere with other headers – Lou Franco Oct 26 '17 at 00:59
  • How do you get the filename, i.e. `httpBody`, in `handleEventsForBackgroundURLSession`? I am assuming that this is called when background transfer is finished. This might be its own question. – siavashk Oct 27 '17 at 18:11
  • task.originalRequest should have it – Lou Franco Oct 27 '17 at 18:33
  • The signature does not include a `NSURLSessionTask` as an argument. It is `- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler` – siavashk Oct 27 '17 at 18:35
  • 1
    Look at https://stackoverflow.com/questions/32676352/urlsessiondidfinisheventsforbackgroundurlsession-not-calling-objective-c -- you have to get the session back, then you can get tasks. – Lou Franco Oct 27 '17 at 18:56
  • Thanks for helping me. Can you recommend a resource for reading about background transfer? Apple’s documentation is not exactly descriptive when it comes to details. – siavashk Oct 27 '17 at 19:43