iOS应用程序运行后台任务时间。

时间:2021-09-25 20:52:21

So I'm performing a background task in my app, which is started like this:

我在我的应用程序中执行一个后台任务,它是这样开始的:

private func someFunc() 
{
    backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        // Present a local Notification
    })

    // Present a different local Notification

    DDLogInfo("Is in bg: \(UIApplication.sharedApplication().applicationState == .Background)")
    DDLogInfo("BG-time remaining: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
}

My problem is that backgroundTimeRemaining always returns 1.79769313486232e+308. This should only happen when the app is active. But this function is only called when the app is running in the background, the first log statement confirms this.

我的问题是,backgroundTimeRemaining总是返回1.79769313486232e+308。这应该只在应用程序处于活动状态时才会发生。但是这个函数只在应用程序在后台运行时调用,第一个日志语句确认了这一点。

I also tried to dispatch the log statement after 5 seconds and disconnecting the device from Xcode (as someone on another SO post suggested), but without success.

我还试图在5秒后发送日志语句并断开设备与Xcode的连接(就像另一个SO post上的人建议的那样),但没有成功。

1 个解决方案

#1


1  

As it turns out, this was a bug in iOS 9.1 and has been fixed since 9.2. If you still encounter this problem, make sure your app is actually running in the background :)

事实证明,这是iOS 9.1中的一个bug,从9.2开始就被修复了。如果您仍然遇到这个问题,请确保您的应用程序实际上正在后台运行:)

#1


1  

As it turns out, this was a bug in iOS 9.1 and has been fixed since 9.2. If you still encounter this problem, make sure your app is actually running in the background :)

事实证明,这是iOS 9.1中的一个bug,从9.2开始就被修复了。如果您仍然遇到这个问题,请确保您的应用程序实际上正在后台运行:)