iPhone后台任务在某个点停止运行
在我的iPhone应用程序中,我有一个后台任务,当应用程序进入后台状态时开始运行。 该任务运行良好,在如下循环中结构化: 跑。 睡5分钟。 跑 睡5分钟 等等 由于某种原因,任务在一定时间后停止运行。。。比如说半小时到一小时 有人能帮我理解为什么吗? 以下是后台任务代码:iPhone后台任务在某个点停止运行,iphone,loops,background,task,Iphone,Loops,Background,Task,在我的iPhone应用程序中,我有一个后台任务,当应用程序进入后台状态时开始运行。 该任务运行良好,在如下循环中结构化: 跑。 睡5分钟。 跑 睡5分钟 等等 由于某种原因,任务在一定时间后停止运行。。。比如说半小时到一小时 有人能帮我理解为什么吗? 以下是后台任务代码: - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"Application entered background s
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"bgCheckSwitch"] == YES) {
//UIApplication* app = [UIApplication sharedApplication];
// Request permission to run in the background. Provide an
// expiration handler in case the task runs long.
NSAssert(bgTask == UIBackgroundTaskInvalid, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case
// the task actually finishes at around the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
[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.
[someClass doSomeThing]; //The actual method performed by the task. The looping is in the method.
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
}
后台任务只运行一段时间,然后操作系统将其终止。这在多任务WWDC10视频中进行了讨论。我认为您总共有5分钟的处理时间来完成任务。这可能会在以后的IOS修订版中增加,我完全忘记了,但我认为这需要10分钟。我知道视频告诉你。这可能很方便:是的,我的答案是被接受的,我可以告诉你,这会让你的应用被拒绝什么会导致应用被拒绝?运行后台任务?您的后台任务如何运行一小时?使用类似的代码,我的任务在一分钟后被终止。本质上,它只是一个不重复的计时器。你是如何实现“5分钟睡眠”的-谢谢