I’m a bit confused about how and when to use beginBackgroundTaskWithExpirationHandler.
Apple shows in their examples to use it in applicationDidEnterBackground delegate, to get more time to complete some important task, usually a network transaction.
When looking on my app, it seems like most of my network stuff is important, and when one is started I would like to complete it if the user pressed the home button.
So is it accepted/good practice to wrap every network transaction (and I’m not talking about downloading big chunk of data, it mostly some short xml) with beginBackgroundTaskWithExpirationHandler to be on the safe side?
If you want your network transaction to continue in the background, then you’ll need to wrap it in a background task. It’s also very important that you call
endBackgroundTaskwhen you’re finished – otherwise the app will be killed after its allotted time has expired.Mine tend look something like this:
I have a
UIBackgroundTaskIdentifierproperty for each background taskEquivalent code in Swift