Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue operation when app did enter background on iOS

in my app i have some NSOperation that update some core data element from a online database, sometime the update require some minute, and when the screen of iPhone lock, the app enter in the background mode, and this update is stopped, so i have to reopen the app to continue the update, so i have search a lot on stack overflow and i have find some information about:

beginBackgroundTaskWithExpirationHandler

that is a method from apple that let continue some task also when the app is in the background mode, and i have do this:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

UIApplication  *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask;

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
}];
}

and now the app continue the task in the background, and seems that all works fine, so my question is, this method i use is safe? or there is a better mode?

thanks

like image 949
Piero Avatar asked Nov 26 '12 23:11

Piero


People also ask

What happens when app goes to background in iOS?

After your app enters the background and your delegate method returns, UIKit takes a snapshot of your app's current user interface. The system displays the resulting image in the app switcher.

How do you make an iOS app run in the background?

For iOS Devices If Background refresh is greyed out in the ON position, go To Settings App - > General - > Background App Refresh - > Turn on the option for the system, and then you can turn on / off by app.

Will iOS terminate the app running in background after a specific time?

At the same time, didReceiveMemoryWarning is invoked for the app. At this point, so that your app continues to run properly, the OS begins terminating apps in the background to free some memory. Once all background apps are terminated, if your app still needs more memory, the OS terminates your app.

How do you run an application continuously running in the background Swift?

You need to enable Background Mode in your project settings under capabilities tab. Under background modes you will find a few modes that satisfy various purposes of running an app in background.


1 Answers

That's not how you do this. Any code that you want to run in the background must be wrapped properly. Something like this:

- (void)someMethodToKeepRunningInBackground {
    UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
        // Uh-oh - we took too long. Stop task.
    }];

    // Perform task here        

    if (taskId != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:taskId];
    }
}

You don't do anything in the UIApplicationDelegate applicationDidEnterBackground: method.

Any task that is wrapped inside the "background task" calls will be allowed to keep running when the app enters the background.

Here's the really important part - the task only gets 10 minutes maximum. If it is still running after 10 minutes your app will be terminated. The expiration handler gives you a few seconds to cleanly end the task before the app is terminated uncleanly.

like image 86
rmaddy Avatar answered Oct 20 '22 22:10

rmaddy