S.J
S.J

Reputation: 3071

Need assistance regarding iOS Background Execution

My ViewController which is presented modally have a save button which save the data in database and then unwind the ViewController.

I am also posting a notification from applicationDidEnterBackground to call the save method so when if user did not tap the save button and and hit the iPhone home button user data gat saved.

Just Like Apple Doc suggests

- (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;
    });
}

Question 1: My task is not that long it will take few seconds to save few hundred rows in DB, is it still necessary to put inside dispatch_async?

Question 2: User can tap save button and immediately hit the iPhone home button in this case my save method will be called twice and it will be bad for my app, should I use dispatch_once or is there any other way?

Question 3 Apple Doc says:

Failure to call the endBackgroundTask: method for a background task will result in the termination of your app. If you provided an expiration handler when starting the task, the system calls that handler and gives you one last chance to end the task and avoid termination.

  1. If my app is running and endBackgroundTask: is not called will iOS crash the app?
  2. What avoid termination means, if my app is in background and my task is also finished still iOS will keep my app alive and will prevent from terminating it?

Question 4 For how long my app will remain functional after going into background while using this backgroudtask? because Apple mention in comments // Start the long-running task and return immediately.

Upvotes: 1

Views: 222

Answers (1)

Gianni Carlo
Gianni Carlo

Reputation: 2462

Question 1

If your operation takes less than 5 seconds (according to Documentation), you can do it without beginBackgroundTaskWithName and dispatch_async, but you have to consider if that data is going to increase with time, if that's the case then you're better off doing it with beginBackgroundTaskWithName and dispatch_async.

Question 2

What I would do, is store a flag in NSUserDefaults about the save operation, so in case the save operation is still underway you can avoid trying to save it again.

Question 3

1.- As stated, your app will be terminated and purged from memory because you're still holding onto resources when you don't need it anymore.

2.- Normally when the user leaves your app, your app can still run some operations in the background from time to time or in response to a push notification if you have that mode enabled. But if your app is terminated by either the user or the OS then you won't be able to

Question 4

Sadly there's no specification about it in the documentation, but you can rest assured it's more than 5 seconds if you use beginBackgroundTaskWithName.

Hope this clears any doubts.

Cheers

-----EDIT 1-----

As stated by @Paulw11, For Question 4, "it is currently 3 minutes and you can examine the UIApplication property backgroundTimeRemaining to determine how much time is left."

Upvotes: 2

Related Questions