Reputation: 14304
I need to check backend via API for app settings update, therefore I'm not sure would it be sufficient to perform such check upon launching or I need to do as soon as app comes to the foreground. Of course it depends on how many apps are opened, device capabilities, but what is there a general rule about how long an app can survive in the background until it's killed? A week?
Upvotes: 3
Views: 6874
Reputation: 368
Apple tells us to use the following method
Listing 3-1 Starting a background task at quit time
- (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ // Clean up any unfinished task business by marking where you // stopped or ending the task outright. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task, preferably in chunks. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }
But this method will not allow to keep running forever
Note: Always provide an expiration handler when starting a task, but if you want to know how much time your app has left to run, get the value of the backgroundTimeRemaining property of UIApplication.
So you will need to use one of the following techniques
Implementing Long-Running Tasks
For tasks that require more execution time to implement, you must request > specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
- Apps that play audible content to the user while in the background, such as a music player app
- Apps that record audio content while in the background
- Apps that keep users informed of their location at all times, such as a navigation app
- Apps that support Voice over Internet Protocol (VoIP)
- Apps that need to download and process new content regularly
- Apps that receive regular updates from external accessories
- Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services.
Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.
In the last method u are limited to what you are able to do
You can read about implementing long-running tasks here
Upvotes: 4
Reputation: 9337
There is no general rule and can not give you any time approximation. Moreover probably for you is more important how much time system can give you for a background fetch as in background app goes to suspended state.
For the time you can request after going to background if I remember correctly before iOS 7 it was up to 10 minutes, on iOS 7 it was up to 180 seconds and I haven't checked it personally on newer versions. Anyway that time is not guaranteed.
Upvotes: 3