gpt4 book ai didi

iPhone 后台任务在某个时刻停止运行

转载 作者:行者123 更新时间:2023-12-03 21:10:04 26 4
gpt4 key购买 nike

在我的 iPhone 应用程序中,我有一个后台任务,当应用程序进入后台状态时,该任务开始运行。该任务运行良好,并且结构如下所示:

运行。睡5分钟。运行。睡5分钟。

等等

由于某种原因,任务在一定时间后停止运行......比如半小时到一个小时。

谁能帮我理解为什么?这是后台任务代码:

- (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 视频对此进行了讨论。

关于iPhone 后台任务在某个时刻停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3725240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com