【问题标题】:UIBackgroundTask not endingUIBackgroundTask 没有结束
【发布时间】:2015-04-20 07:34:24
【问题描述】:

我正在使用 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 张。但这个过程并没有停止。并且应用程序不断上传剩余的图片。

【问题讨论】:

    标签: ios objective-c cocoa-touch uikit uibackgroundtask


    【解决方案1】:

    您不应该依赖backgroundTimeRemaining。 苹果表示,当你在后台运行应用程序时,应用程序可能会被杀死:

    此属性包含应用程序必须在 后台可能会被系统强杀。

    实际上,您可以有几秒钟或 5 分钟的时间,这取决于许多因素,例如电池寿命、内存使用情况等。 无论 backgroundTimeRemaining 显示什么,您都必须对应用程序进行编程以执行正确的操作。

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2018-12-01
      • 2019-11-08
      相关资源
      最近更新 更多