Alamofire Request Gets Stuck When Entering Background

Alamofire request gets stuck when entering background?

You can use background fetching to solve this problem. It can be done in the following way 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 your alamofire service after this line. In the completion handler, end the task using the below lines.

    UIApplication.shared.endBackgroundTask(self.backgroundTask!)
self.backgroundTask = UIBackgroundTaskInvalid

Please note that the app has some background time (backgroundTimeRemaining property) remaining before it enters the inactive state. You have to get your task done before that time. The handler is called shortly before the remaining background time reaches zero. Also, each call to the method beginBackgroundTask(withName:){} must be balanced by a matching call to the endBackgroundTask: method.

To make the code given above work, you need to adjust settings in your app. Go to "Targets" and click on "Capabilities" to make the following changes

Changes to made in "Target" section

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

Sample Image

I hope this helps you. If you need more detailed information, these links might help

  • https://developer.apple.com/reference/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio
  • https://developer.apple.com/reference/uikit/uiapplication/1622970-endbackgroundtask

Alamofire request stuck on return to foreground?

It turns out I misdiagnosed the issue. The Alamofire request was still running and resuming when brought to foreground. The issue was really our UIRefreshControl got stuck due to a bug in the UIRefreshControl UIKit library. Implemented a workaround for that issue and we were good.

App freezing when Alamofire is downloading

By default, Alamofire dispatches the response handler on the main queue (source). However, you can specify a different one using the queue: parameter of the response() method to keep the UI responsive:

    Alamofire.request(.GET, urlDomain + "_table/e21?offset=\(nextCheck)", headers: headers, encoding: .JSON)
.responseJSON(queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { response in

Keep in mind that if you do UI updates in the response handler, you need to do in in the main queue:

dispatch_async(dispatch_get_main_queue()) {
// update some UI
}

Alamofire requests while device is sleeping

Yes, background sessions only permit upload and download tasks, not data tasks. They also only permit delegate-based requests, not completion handler-based requests. This answer outlines many of the considerations when doing this in conjunction with Alamofire.

But this begs the question as to whether you really want to use a background session at all. When your app is awaken for background fetch, if you’re able to finish you request within a reasonable amount of time (e.g. 30 seconds), you should probably consider a standard session, not a background session. It’s a lot simpler.

Do not conflate an app running in the “background” with a “background” URLSessionConfiguration: They’re completely different patterns. Just because your app is running in the background, that doesn’t mean you have to use background URLSessionConfiguration. If your app is running (whether in foreground or in the background), then a standard session is fine. You only need background session if you want it to continue after the app is suspended (or is terminated) and you’re willing to encumber yourself with all the extra overhead that background sessions entail.

Background sessions are not intended for requests performed while the app is running in the background. They’re intended for requests that will continue after your app is suspended (and even if it eventually is terminated in the course of its natural lifecycle). That means that background sessions are ideal for slow requests that cannot be completed in a reasonable amount of time, e.g., downloading video asset, downloading many large image assets or documents, etc.

But if you’re just performing a routine GET/POST request that will complete in a reasonable amount of time, consider not using background URLSessionConfiguration, but just do normal request and call the background fetch completion handler when your request is done (i.e., in your network request’s completion handler).



Related Topics



Leave a reply



Submit