backgroundTimeRemaining的这个大数字是多少?

sun*_*hun 3 background ios

我刚刚进入iOS编程,并且正在努力解决很多问题.

我正在尝试实现一小段代码来获取当前位置并在后台将其发送到服务器.当我打电话时beginBackgroundTaskWithExpirationHandler,我发现backgroundTimeRemaining财产的回报如此之大.查看代码下面的日志.

if (self.backgroundTask == UIBackgroundTaskInvalid) {
        NSLog(@"***** startBackground work");

        UIApplication *app = [UIApplication sharedApplication];

        self.backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
            NSLog(@"Background handler called. Not running background tasks anymore.");
            [app endBackgroundTask:self.backgroundTask];
            self.backgroundTask = UIBackgroundTaskInvalid;
        }];

        DebugLog(@"====>backgroundTimeRemaining:%.1f seconds", app.backgroundTimeRemaining);
        if (timer == nil) {
            timer = [NSTimer scheduledTimerWithTimeInterval:10 * 60
                                                     target:self
                                                   selector: @selector(timerFired:)
                                                   userInfo:nil
                                                    repeats:YES];
        }
    }
Run Code Online (Sandbox Code Playgroud)

日志:

2013-11-29 09:23:14.852 JeeneeLocatorService[1666:70b] <JLSViewController.m:(317)> ====>backgroundTimeRemaining:179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0 seconds
Run Code Online (Sandbox Code Playgroud)

它看起来不正常,我已经从iOS开发者库网站iOS上读取了大约10分钟,即使我打电话 beginBackgroundTaskWithExpirationHandler做很长时间的后台任务.

我正在使用XCode5中的iOS7模拟器进行测试.我是否可以将大量时间用于后台工作.你的回答将非常感激.

编辑:得到答案后,我将剩余时间显示代码移动到timerFired:方法.

- (void)timerFired:(NSTimer *)timer {
        DebugLog(@"***** Timer fired *****");

        UIApplication *app = [UIApplication sharedApplication];
        DebugLog(@"====>backgroundTimeRemaining:%.1f seconds", app.backgroundTimeRemaining);
 }
Run Code Online (Sandbox Code Playgroud)

但是,每当调用timerFired:方法时,它仍然会给我相同的时间.这不适用于模拟器吗?

Dar*_*ren 8

作为您对ios编码的新手,一点提示:按住ALT并单击backgroundTimeRemaining.它告诉你它返回一个双精度的NSTimeInterval,所以使用%f不是问题.

如果你单击弹出窗口的底部蓝色链接,它将打开文档,你会看到如果应用程序实际上不在后台,这个数字会非常大.

所以我猜这是你的问题,当打印NSLog时你的应用程序就在前台.

  • 并且使用`%.1f`打印`DBL_MAX`会产生上述输出. (2认同)