首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么beginBackgroundTaskWithExpirationHandler会提前结束?

为什么beginBackgroundTaskWithExpirationHandler会提前结束?
EN

Stack Overflow用户
提问于 2013-12-21 20:14:12
回答 1查看 3.2K关注 0票数 1

我正在努力使它正确工作,但它似乎总是提前174秒(2.9分钟)结束。我一直在在线上学习关于如何使用beginBackgroundTaskWithExpirationHandler的每一个教程,我没有发现我的代码有什么问题。我要这个在8.5分钟结束。在调用到期处理程序之前,endBackgroundTask方法甚至都不会得到调用。这有什么问题吗?

代码语言:javascript
复制
- (void)applicationDidEnterBackground:(UIApplication *)application {

        if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
    {
        NSLog(@"Multitasking Supported");

        backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^ {
            NSLog(@"Terminated");

            //Clean up code. Tell the system that we are done.
            [application endBackgroundTask: backgroundTask];
            backgroundTask = UIBackgroundTaskInvalid;
        }];

        timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                 target:self
                                               selector:@selector(count:)
                                               userInfo:nil
                                                repeats:YES];
    }
    else
    {
        NSLog(@"Multitasking Not Supported");
    }

}



-(void)turnOffDesktops:(NSTimer *)time {
//do smth
if(count < (60*8.5)){
    NSLog(@"%d",count);
    count++;
}else{

    [application endBackgroundTask: backgroundTask];
    backgroundTask = UIBackgroundTaskInvalid;

    [timer invalidate];
    timer = nil;
    count = 0;
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-21 20:18:58

苹果从来没有承诺过你将被允许多长时间来执行你的背景任务。历史上(直到iOS7),应用程序通常在后台运行10分钟。不再是这种情况了!关注WWDC 2013年关于背景的视频。随着在NSURLSession中添加了新的下载和上传API (能够在外部专用守护进程上安排下载和上载任务),苹果大大减少了允许的后台时间。他们这么做是因为这个API一直是用来下载和上传的,而不是后台的任意任务。

您可以通过查询- [UIApplication backgroundTimeRemaining]来确定背景中剩下的时间量。您可以使用它来安排您的代码在最晚可能的时候开始。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20723112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档