1

My question might be a bit stupid but I am a bit confused after working for the first time with iOS.

I am currently working with Swift and want to do a Post request from the background. To achieve this, I'm using Alamofire and so far its no problem to do a post request with a JSON body which contains Data that the WebApi writes into a Database but I am worried about the timelimit with a lot of Data to send.

Therefore the Web API got Methods like the following:

AddData(Data[] data){ ...} 

the matching Json for example looks like this

[[
"Id" : "65400000-0000-0000-0000-000000000002",
 "SomeProperty" : null
]] 

representing an Array of Type Data containing just one Data object.

Question: Is it possible to send a request matching the aforementioned scenario ( a request containing the JSON object in the body not the actual file) from the Background, with the given fact that in iOS it's just possible to do a background upload from a file when the app is suspended after the timelimit is reached.

Malik
  • 3,763
  • 1
  • 22
  • 35
JSTW1212
  • 75
  • 3
  • when, like starting or exiting the app, do you do these operations? Why are you worried that it gets suspended? – dirtydanee Jan 03 '17 at 15:35
  • 1
    NSURLSession has abilities to complete network activities on your behalf if the app goes to the background, but if you use registerBackgroundTaskHandler then your app has an (undocumented) 3 minutes in which it can execute in the background, that is plenty of time to perform an upload. – Gruntcakes Jan 03 '17 at 15:39
  • This article gives a good overview of using NSURLSession for background upload/download tasks: http://www.appcoda.com/background-transfer-service-ios7/. The code samples are in Objective-C but the concepts are explained really well, including how to set the background task entitlement in your app settings. Once you are familiar with the concepts I think you'll be able to set the correct entitlement for your app, and it'll be just a case of finding a Swift 3 example of setting up the actual request with NSURLSession or Alamofire. – Natalia May 18 '17 at 02:25
  • This gives a good overview of the code setup to use Alamofire requests in the background http://stackoverflow.com/a/31751337 – Natalia May 18 '17 at 02:25

0 Answers0