Alamofire query getting stuck on typing background?

I use Alamofire to call a web service that takes quite a while to download. If the application goes into the background, I am stuck with my bootloader when I return to the application. I guess this is because the call never returns anything to my completion handler. How can I solve this problem?

+5
source share
1 answer

You can use background extraction to solve this problem. This can be done as follows in Swift 3:

var backgroundTask: UIBackgroundTaskIdentifier? // global variable backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "backgroundTask") { // Cleanup code should be written here so that you won't see the loader UIApplication.shared.endBackgroundTask(self.backgroundTask!) self.backgroundTask = UIBackgroundTaskInvalid } 

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.

Changes to the section

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

enter image description here

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

+4
source

Source: https://habr.com/ru/post/1258680/


All Articles