我正在使用UIBackgroundtaskIdentifier创建后台任务,上传图像到服务器。
NSLog(@"Time left = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);当我把应用程序带到后台时,它显示应用程序有179秒的时间在后台运行。当服务器逐个上传图像时,我会记录服务器的响应。
[[DataCoordinator sharedInstance] setBackgroundTask:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"No background task should run now.");
[[UIApplication sharedApplication] endBackgroundTask:[[DataCoordinator sharedInstance] backgroundTask]];
[[DataCoordinator sharedInstance] setBackgroundTask:UIBackgroundTaskInvalid];
}]];这里发生的奇怪的事情是,在iOS为我的应用程序在后台运行提供的时间结束后,上传仍然没有停止。假设我尝试上传33张图片,当179秒完成时,应用程序只成功上传了33张图片中的9张。但是这个过程并没有停止。和应用程序继续上传剩余的图片。
发布于 2015-04-20 15:45:21
你不应该依赖backgroundTimeRemaining。苹果公司表示,当你在后台运行应用程序时,应用程序可能会被杀死:
此属性包含应用程序在被系统强制终止之前必须在后台运行的时间量。
在现实中,你可以有几秒钟或5分钟,这取决于许多因素,如电池寿命,内存使用率等。无论backgroundTimeRemaining显示什么,你都必须编写应用程序来做正确的事情。
https://stackoverflow.com/questions/29741761
复制相似问题