swift/ios refreshing app data when in background
I’m writing a iOS/Swift application which reads data from a REST service each X minutes and updates the UI accordingly.
Now I would like that when the app is put in the background, a task keeps being invoked at X minutes intervals reading from the REST service and, in case the data just read satisfies a given condition, show a notification prompting the user to bring the app back to the foreground.
In my searches I’ve read that during
applicationDidEnterBackground event, I should start a task with
The problem is that, if I’ve understood correctly, this allows a maximum of 10/15 minutes after which the app is terminated if the task is not stopped with
endBackgroundUpdateTask, while I want the task to keep polling the service indefinitely (at least until the user disable it from the app’s settings)
My question is:
How is this kind of functionality performed normally? Do some common solutions or best practices exist for the solution of such a problem?
- Running a app forever in the background in iOS 8
- Creating an animated gradient background similar to the Solar Weather Application in xCode
- iOS Swift - Reload location function with NSTimer for app background doesn't work
- iOS long-running background timer with “location” background mode
- iOS app icon with transparent background showing black background on device
- How to set a background image in Xcode using swift?
3 Solutions Collect From Internet About “swift/ios refreshing app data when in background”
Use iOS Background Fetch feature where you can specify minimum background fetch interval. But actual interval between successive invocation of your code will be determined by iOS framework. For details checkout this link: http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch–mobile-20520
I use this approach in my app and I think it is a preferred way of doing.
You can use a local notification that can be presented from the background in case your condition is met.
Correct, iOS will eventually shut down the background process, you can’t enforce continuous background activity. Use the
backgroundTimeRemainingproperty to check how much time your application has left and try to handle it as gracefully as possible by calling
endBackgroundTaskso that iOS does not force kill your app.
As a solution, you could think about using remote notifications with with
content-available : YES, which runs the didReceiveRemoteNotification
Have a look at the Parse.com Their local datastore is an abstraction for what you are trying to acheive.
By the way, is it really necessary to refresh in the background. If call is relatively quick, there is no need to refresh until the user open’s the app. Background processes like that, using the net can be quite battery consuming when the user are not on a Wifi. So consider the use case carefully!
- #warning: C-style for statement is deprecated and will be removed in a future version of Swift
- Swift – Instantiating a navigation controller without storyboards in App Delegate
- Change Language in the app programmatically
- Handling non-ascii characters in textField:shouldChangeCharactersInRange:replacementString:
- Hide status bar while scrolling
- Control more than one button using tags
- Error when trying to obtain a certificate: The specified item could not be found in the keychain
- Swift AVAudioEngine crash: player started when in a disconnected state
- SWRevealViewController – RightViewController
- Swift Encode/decode emojis
- CGContext: how do I erase pixels (e.g. kCGBlendModeClear) outside of a bitmap context?
- Objective-C, Need help creating an AVAudioPlayer singleton
- Save UIImage, Load it in Wrong Orientation
- CGBitmapContextCreate: unsupported parameter combination. How to pass kCGImageAlphaNoneSkipFirst
- simple Swift generics puzzle