This question already has an answer here:
这个问题在这里已有答案:
- backgroundTimeRemaining returns (35791394 mins)? 2 answers
backgroundTimeRemaining返回(35791394分钟)? 2个答案
I have just entered into iOS programming and am struggling with many things.
我刚刚进入iOS编程,并且正在努力解决很多问题。
I am trying to implement small piece of code to getting current location and send it to server in the background. When I call beginBackgroundTaskWithExpirationHandler
, I found that backgroundTimeRemaining
property returns so big number. Look at the log below the code.
我正在尝试实现一小段代码来获取当前位置并在后台将其发送到服务器。当我调用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];
}
}
Log:
2013-11-29 09:23:14.852 JeeneeLocatorService[1666:70b] <JLSViewController.m:(317)> ====>backgroundTimeRemaining:179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0 seconds
It does not look normal and I have read from iOS developer library site iOS allows about 10 minutes even though I call beginBackgroundTaskWithExpirationHandler
to do long time background task.
它看起来并不正常,我已经从iOS开发者库网站iOS上读取了大约10分钟,即使我调用beginBackgroundTaskWithExpirationHandler来做长时间的后台任务。
I am testing with iOS7 simulator in XCode5. Is it true that I can have that much of time for background job. Your answer would be very appreciated.
我正在使用XCode5中的iOS7模拟器进行测试。我是否可以将大量时间用于后台工作。你的回答将非常感激。
EDIT: After getting answer, I move the remaining time display code into the timerFired: method.
编辑:得到答案后,我将剩余时间显示代码移动到timerFired:方法。
- (void)timerFired:(NSTimer *)timer {
DebugLog(@"***** Timer fired *****");
UIApplication *app = [UIApplication sharedApplication];
DebugLog(@"====>backgroundTimeRemaining:%.1f seconds", app.backgroundTimeRemaining);
}
But, it still gives me same time left whenever timerFired: method is called. Does not this work just in simulator?
但是,只要调用timerFired:方法,它仍然会给我相同的时间。这不适用于模拟器吗?
1 个解决方案
#1
7
As your new to ios coding, a little tip: Hold ALT and click on backgroundTimeRemaining. It tell's you it returns an NSTimeInterval which is a double, so using %f is not the problem.
作为您对ios编码的新手,提示:保持ALT并单击backgroundTimeRemaining。它告诉你它返回一个双精度的NSTimeInterval,所以使用%f不是问题。
If you click the bottom blue link of the popup it'll open the docs and you'll see it says that this figure will be very large if the app isn't actually in the background.
如果你单击弹出窗口的底部蓝色链接,它将打开文档,你会看到它说如果应用程序实际上不在后台,这个数字会非常大。
So I'm guessing this is your issue, that your app is in the foreground when the NSLog is printed.
所以我猜这是你的问题,当打印NSLog时你的应用程序在前台。
#1
7
As your new to ios coding, a little tip: Hold ALT and click on backgroundTimeRemaining. It tell's you it returns an NSTimeInterval which is a double, so using %f is not the problem.
作为您对ios编码的新手,提示:保持ALT并单击backgroundTimeRemaining。它告诉你它返回一个双精度的NSTimeInterval,所以使用%f不是问题。
If you click the bottom blue link of the popup it'll open the docs and you'll see it says that this figure will be very large if the app isn't actually in the background.
如果你单击弹出窗口的底部蓝色链接,它将打开文档,你会看到它说如果应用程序实际上不在后台,这个数字会非常大。
So I'm guessing this is your issue, that your app is in the foreground when the NSLog is printed.
所以我猜这是你的问题,当打印NSLog时你的应用程序在前台。