Ios UIApplicationIdentinterBackgroundNotification剩余执行时间
在我使用iOS 9.2、Swift 2.1的应用程序中,当应用程序转到后台时,我需要将一些数据保存到核心数据中。为此,我在Ios UIApplicationIdentinterBackgroundNotification剩余执行时间,ios,swift,Ios,Swift,在我使用iOS 9.2、Swift 2.1的应用程序中,当应用程序转到后台时,我需要将一些数据保存到核心数据中。为此,我在uiapplicationidenterbackgroundnotification通知的调用路径中注册了每个视图控制器,每个视图控制器都有一个用于保存各自数据的实例方法 我在多个地方读到,默认情况下,该应用程序有大约5秒的时间来完成执行,因此我们需要使用beginBackgroundTaskWithExpirationHandler将其延长到大约5分钟。下面是响应上述通知的
uiapplicationidenterbackgroundnotification
通知的调用路径中注册了每个视图控制器,每个视图控制器都有一个用于保存各自数据的实例方法
我在多个地方读到,默认情况下,该应用程序有大约5秒的时间来完成执行,因此我们需要使用beginBackgroundTaskWithExpirationHandler
将其延长到大约5分钟。下面是响应上述通知的选择器方法的示例
func applicationEntersBackground()
{
print("Before Extension: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
let taskID = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil)
print("During Extension: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
saveCoreData()
if(taskID != UIBackgroundTaskInvalid)
{
UIApplication.sharedApplication().endBackgroundTask(taskID)
}
print("After Extension: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
}
下面是print()语句的结果
Before Extension: 179.933103708318
During Extension: 179.930266333336
After Extension: 179.922843541659
我的怀疑是
beginBackgroundTaskWithExpirationHandler
对剩余时间没有任何影响applicationEntersBackground
方法返回,类似的通知将发送到另一个VC的相应方法。假设180秒是总的延长时间,VC1在通知处理上花费了大约10秒,那么VC2通知处理程序在其beginBackgroundTaskWithExpirationHandler-endBackgroundTask
调用之间是否有大约170秒的时间李>
endBackgroundTask
时,5秒计数器(如果它是真的)是否会恢复正常,并可能在下一个VC收到通知之前终止应用程序背景时间:
当应用程序在前台运行时,此属性中的值保持适当的大。如果应用程序使用beginBackgroundTaskWithExpirationHandler:方法启动一个或多个长时间运行的任务,然后转换到后台,则此属性的值将调整以反映应用程序剩余的运行时间
回答您的问题:
backgroundTimeRemaining
在应用程序位于前台时保持在180左右,因此您可以知道启动后台任务后的时间。此值不是允许您在没有后台任务的情况下运行多长时间的指标
beginBackgroundTaskWithExpirationHandler
产生了影响,正如您所看到的,剩余时间减少了(减少了一个小值,因为该方法不需要太多时间)
这里重要的是调用beginBackgroundTaskWithExpirationHandler
和调用endBackgroundTask
之间经过的时间。如果不超过180秒的限制,您可以根据需要分配通话间隔时间
调用endBackgroundTask
后,应用程序将被挂起,无论是2秒还是179秒
您可以了解有关应用程序进入后台的更多详细信息。我建议您仔细阅读文档,它可能会澄清您在这件事上可能遇到的其他问题 通过查看背景剩余时间的
当应用程序在前台运行时,此属性中的值保持适当的大。如果应用程序使用beginBackgroundTaskWithExpirationHandler:方法启动一个或多个长时间运行的任务,然后转换到后台,则此属性的值将调整以反映应用程序剩余的运行时间
回答您的问题:
backgroundTimeRemaining
在应用程序位于前台时保持在180左右,因此您可以知道启动后台任务后的时间。此值不是允许您在没有后台任务的情况下运行多长时间的指标
beginBackgroundTaskWithExpirationHandler
产生了影响,正如您所看到的,剩余时间减少了(减少了一个小值,因为该方法不需要太多时间)
这里重要的是调用beginBackgroundTaskWithExpirationHandler
和调用endBackgroundTask
之间经过的时间。如果不超过180秒的限制,您可以根据需要分配通话间隔时间
调用endBackgroundTask
后,应用程序将被挂起,无论是2秒还是179秒
您可以了解有关应用程序进入后台的更多详细信息。我建议您仔细阅读文档,它可能会澄清您在这件事上可能遇到的其他问题 (1)通知名为UIApplicationIdentinterBackgroundNotification
,我认为这表明应用程序已经在后台。此外,在前台执行期间打印剩余时间的值为1.79769313486232e+308。(2) 我怀疑这通电话的目的(即推迟暂停和增加剩余时间)。我认为它与调用本身的执行时间混淆了。(4)问题是,是否有多个连续调用来自不同控制器的beginBackgroundTaskWithExpirationHandler和endBackgroundTask
对,在VC1调用endBackgroundTask
和VC2调用beginBackgroundTaskWithExpirationHandler
之间的间隔期间,应用了什么规则。(1)通知名为UIApplicationIdentinterbackgroundNotification
,我认为这表明应用程序已经在后台。此外,在前台执行期间打印剩余时间的值为1.79769313486232e+308。(2) 我的工作