1

is it possible not to pause the application while in background mode (when you press the home button and the app minimizes)? I have some timers and variables that i don't want to get paused.

EDIT:

I have followed this example http://evilrockhopper.com/2010/01/iphone-development-keeping-the-ui-responsive-and-a-background-thread-pattern/

I have called a timer inside however it's not getting called when i enter background mode:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if(self.viewController.timerquest != NULL)
    {
        if(self.viewController.timerquest.timerRunning){
            // Save varibales
            [self performSelectorInBackground:@selector(performLongTaskInBackground) withObject:nil];
        }
    }
}



- (void) performLongTaskInBackground
{
    // Set up a pool for the background task.
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // perform some long task here, say fetching some data over the web.
    //...
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

    // Always update the components back on the main UI thread.
    [self performSelectorOnMainThread:@selector(completeLongRunningTask) withObject:nil waitUntilDone:YES];

    [pool release];
}

-(void) updateTimer{

    // Update my timer. This method is not being called in background mode
}

What should I do?

Thanks.

2
  • In that example, the "background" he refers to is simply a background thread that runs concurrently to the main UI thread. It allows you to do complex computations without freezing your UI. It does not, however, allow your program to keep working as long as it wants when your app is in the background. You are allowed only a very limited amount of time to run after the user leaves you.
    – matthias
    Aug 30, 2011 at 15:33
  • I believe [this][1] is what you're trying to do. You need to dispatch the task you want to do asynchronously, and provide a handler to gracefully stop the task when iOS pauses your app. [1]developer.apple.com/library/ios/#documentation/iphone/…
    – matthias
    Aug 30, 2011 at 15:36

4 Answers 4

2

use Long Running Background tasks according to manual: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html

9
  • You mean Completing a Finite-Length Task in the Background instead? I don't see anything usefull in Long Running Background tasks, it all about GPS locations and audio which i really don't care about. Aug 29, 2011 at 21:37
  • @Marc yes, Finite-Length if it fits more your case Aug 29, 2011 at 21:39
  • I don't know if I understood it correct, but if I have some timers in my controller which I need to not pause while in background mode I have to protect those by calling beginBackgroundTaskWithExpirationHandler or what? Has anyone some experience with this? By the way, the example there isn't very clear. Aug 29, 2011 at 21:47
  • see some example: evilrockhopper.com/2010/01/… Aug 29, 2011 at 21:49
  • Hmm this examples shows how to perfom something in the background like downloading, I just want to continue to run something in the background (a timer in a class in the viewcontroller). Aug 29, 2011 at 21:57
1

Have a read here at the Apple non-technical documentation or at the technical reference.

1

Could you replace the timer with delayed background notifications?

2
  • The timer is for runnning a countdown only. Aug 29, 2011 at 22:43
  • @Marc a countdown to what? If it's something the user should know about when it finishes, it sounds like a perfect candidate for local notifications.
    – matthias
    Aug 30, 2011 at 15:44
1

Depending on what happens when your timers fire, you want to set up a local notification that fires at the same time the timer would have; this is useful when the timer would present something for the user to act on. As far as saving variables, you'll want to use -applicationDidEnterBackground: to save whatever state you need to, so that the correct variables can be loaded/generated when the app relaunches (which may not happen until the app has been exited and completely restarted again).

The types of tasks that are allowed to perform long running background tasks are pretty limited, specifically for things like GPS and playing audio. Everything else needs to decide on a task-by-task basis whether to simulate continued running (such as turning a timer to a local notification), pausing and saving necessary state to continue the next time the app is run, simply cancelling the task and gracefully restarting/notifying the user upon resuming the app, or asking for a finite length of time to finish a task (for things like finishing a download).

1
  • The edit to your post does not cover apps entering the background. Your best bet is to decide whether to turn your timer into a notification for your user, or to pause it and resume it when the user returns--you can even simulate what would have happened at each timer pulse as part of resuming.
    – matthias
    Aug 30, 2011 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.