2

How to cancel all Alamofire requests when user entered background or app is terminated?

I have researched everything on the Internet including these Stack Overflow links;

I have tried;

Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
    dataTasks.forEach { $0.cancel() }
    uploadTasks.forEach { $0.cancel() }
    downloadTasks.forEach { $0.cancel() }
}

But tasks are always empty, eventhough there are some.

And also I have tried this;

Alamofire.SessionManager.default.session.invalidateAndCancel()

It invalidates the session but revalidation or recreating the session is not working as expected.

How can I stop all active Alamofire requests completely? Any help would be appreciated.

Nilay Dagdemir
  • 236
  • 2
  • 15
  • If the app is terminated then the network request and all running process of the app stops immediately. The same applies when the app enters background, it freezes all running process, only a remote push can trigger code execution as I know. How do you know that your request is not canceled? – balazs630 Jul 03 '19 at 14:20
  • Possible duplicate of [Cancel a request Alamofire](https://stackoverflow.com/questions/41478122/cancel-a-request-alamofire) – Ratul Sharker Jul 03 '19 at 14:52
  • Hello @Ratul Sharker, as I already said in the question, unfortunately that answer didn't solve my problem. – Nilay Dagdemir Jul 04 '19 at 06:02
  • Hello @balazs630, when I return to my app from background, I see that some of my upload operations have already finished on the background. Also my print states which placed in my request, still printing out for some time, on the background. So my requests are still valid even on the background, but only for some time. – Nilay Dagdemir Jul 04 '19 at 06:05

0 Answers0