开发者

Objective-c Don't pause while entering in background

开发者 https://www.devze.com 2023-03-31 09:11 出处:网络
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.

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 ex开发者_运维问答ample 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.


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


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


Could you replace the timer with delayed background notifications?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号