Service Not working when app goes background

Hi,

I used URLSession to call Api.


Code sample :

let defaultSessionConfiguration = URLSessionConfiguration.default

defaultSessionConfiguration.timeoutIntervalForRequest = 600

defaultSessionConfiguration.timeoutIntervalForResource = 600

defaultSessionConfiguration.urlCache = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)

defaultSessionConfiguration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData


let defaultSession = URLSession(configuration: defaultSessionConfiguration)


let task = defaultSession.dataTask(with: urlRequest) { data, response, error in

...

}

task.resume()

}


When service was called and it was not completed,I pressed home button.

So app went to background state.

And Api call was failed....


How to make Api call to get success in this scenerio and what is reason for api call failure?

Thanks in advance for the supporter.

Accepted Reply

How to make my webApi call to get success even when app goes to background state?

Please read this post and then write back if you have further questions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

WHere do you put this code ?


can you show your complete code for

func applicationWillEnterForeground(_ application: UIApplication) { }


and for


func applicationDidBecomeActive(_ application: UIApplication) { }


and


func applicationWillEnterForeground(_ application: UIApplication) {

Hi

I put this code in a separate swift file.

I didnt implement anything in applicationWillEnterForeground and in applicationDidBecomeActive.


when app moves to background and after few seconds when it comes to foreground,my webApi call is failed.

By surfing the internet i understood that ,after app moves to background,and with in few more seconds it will beforcibly moved to suspened state.

So my webApi call failed.

How to make my webApi call to get success even when app goes to background state?

How to make my webApi call to get success even when app goes to background state?

Please read this post and then write back if you have further questions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"