简体   繁体   English

Objective-c在进入背景时不要暂停

[英]Objective-c Don't pause while entering in background

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/ 我遵循了这个示例http://evilrockhopper.com/2010/01/iphone-development-keeping-the-ui-response-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. 谢谢。

在此处阅读Apple非技术文档技术参考

您可以用延迟的后台通知替换计时器吗?

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). 至于保存变量,您将需要使用-applicationDidEnterBackground:来保存所需的任何状态,以便在应用重新启动时可以加载/生成正确的变量(只有在应用退出并完全退出时才可能发生)重新启动)。

The types of tasks that are allowed to perform long running background tasks are pretty limited, specifically for things like GPS and playing audio. 允许执行长时间运行的后台任务的任务类型非常有限,特别是对于GPS和播放音频之类的事情。 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). 其他所有内容都需要根据任务逐一决定是否模拟继续运行(例如将计时器转到本地通知),暂停并保存必要的状态以在下次运行应用程序时继续运行,只需取消任务并在恢复应用程序后正常重启/通知用户,或要求有限的时间来完成任务(例如完成下载等操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM