File Upload in Background using BGTaskSchedule

I have implemented both BGAppRefreshTaskRequest & BGProcessingTaskRequest. I use the AppRefresh for data refresh on the client and small dB updates using RESTful APIs running on an AWS EC2 instance running Django. Users of my iOS app cannot rely on access to the internet when it is in use, so I must queue them to run in background when they are near a WiFi network. The BGAppRefreshTaskRequests to the host are made using NSURLSessionDownloadTasks, as according to Apple Documentation DataTasks are not supported in BGTaskSchedule.

I use ProccessingTask to upload a .jpg file to AWS S3. From the iOS device I execute a HTTP GET NSURLRequest to the host, which returns a dictionary that includes the S3 url, the S3 signature, etc. Per instructions from AWS's web site I create a multipart/form-data NSMutableURLRequest with a boundary string.

I then form a NSURLSessionUploadTask setting the URL and HTTP POST as a multipart/form-data NSMutableURLRequest with a boundary string. I check the response from AWS S3 using the URLSession:session task:didCompleteWithError NSURLSessionTaskDelegate. In analyzing the NSURLResponse it is clear that the system executes a repeat of the NSURLSessionDownloadTask rather than the NSURLSessionUploadTask. There is no response from the AWS S3 host, not even a returned error message.

So, my questions are:

  1. Is NSURLSessionUploadTask allowed with BGTaskSchedule? If not, should I use NSURLSessionDownloadTask with multipart/form-data to upload a file?
  2. If NSURLSessionUploadTask is allowed, what could be causing the iOS System to repeat a previous NSURLSessionDownloadTask rather than the file upload?

Prior to POSTing the NSURLSessionUploadTask I cancel any remaining NSURLSesstionTasks on the session.

The application is written in Objective C as it uses a C++ library of OpenCV.

Thank you, Eric

Answered by eric_s in 789370022

So here is the answer to my own question... I found it more reliable to use NSURLSessionDownloadTask for the purpose of uploading a file to AWS S3. While I remember reading in the "docs" that one could not use NSURLSessionDataTask when operating in background using BGTaskSchedule. I do not recall reading that one could not use NSURLSessionUploadTask. But, Indeed that seems to be the case.... I hope this helps someone else out there.

Accepted Answer

So here is the answer to my own question... I found it more reliable to use NSURLSessionDownloadTask for the purpose of uploading a file to AWS S3. While I remember reading in the "docs" that one could not use NSURLSessionDataTask when operating in background using BGTaskSchedule. I do not recall reading that one could not use NSURLSessionUploadTask. But, Indeed that seems to be the case.... I hope this helps someone else out there.

as according to Apple Documentation DataTasks are not supported in BGTaskSchedule.

Hmmm, I think you’re getting confused by the terminology here. There are two things using the term background:

These are different things, although in some situations it does make sense to use them together.

Regarding the quote above, data tasks can’t be used in background sessions [1], but they’re fine to use in a standard session when using the Background Tasks framework to get CPU time.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Data tasks can be used in a background session with one critical caveat: They fail if your app gets suspended. There are specific circumstances where this doesn’t matter, and it actually makes sense to run a data task in a background session, but that’s definitely not the standard pattern.

File Upload in Background using BGTaskSchedule
 
 
Q