You can use background extraction to solve this problem. This can be done as follows in Swift 3:
var backgroundTask: UIBackgroundTaskIdentifier?
Call alamofire support after this line. In the completion handler, complete the task using the following lines.
UIApplication.shared.endBackgroundTask(self.backgroundTask!) self.backgroundTask = UIBackgroundTaskInvalid
Please note that the application has some background time ( backgroundTimeRemaining property) remaining before it goes into an inactive state. You must complete your task before this time. The handler is called shortly before the remaining background time reaches zero. In addition, every call to the beginBackgroundTask(withName:){} method must be balanced by matching with the endBackgroundTask: method.
To make the code above, you need to configure the settings in your application. Go to Goals and click on Features to make the following changes.

After that, go to your info.plist file and open it as a source to add the following code.

Hope this helps you. If you need more details, these links may help.
source share