Search code examples
iosnsurlsessionnsurlsessiondownloadtasknsurlsessionuploadtask

Is uploading a file using a download task for background transfert a good thing?


I need to upload files in background for my application.

So i looked uploadtask method but it actually need a fileurl to work with background transfert. It won't really be a big deal to put my object in JSON/XML and save it as a file but it would need a change in my server side.

However, i notice that there is a way to upload file using the downloadtask which additionally return the body response.

Basically, i would create my object in memory and adding it as NSData (byte[]) in my download request. It actually worked but i fear that there may be memory/connection bug since it's not only file on disk but also memory and we are in a background status...

Is this a correct way to do it ?? or i may have connection problem ?

Thanks!


Solution

  • I think it's okay, this is documented by Apple

    Uploading a File Using a Download Task

    To upload body content for a download task, your app must provide either an NSData object or a body stream as part of the NSURLRequest object provided when it creates the download request.

    If you provide the data using a stream, your app must provide a URLSession:task:needNewBodyStream: delegate method to provide a new body stream in the event of an authentication failure. This method is described further in Uploading Body Content Using a Stream.

    The download task behaves just like a data task except for the way in which the data is returned to your app.