1

I am using UIBackgroundtaskIdentifier to create background task of uploading images to server.

NSLog(@"Time left = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);

When I take the application in to background, it show that applicaiton have 179 seconds to run in background. I log the server responses as it uploads that images one by one.

    [[DataCoordinator sharedInstance] setBackgroundTask:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"No background task should run now.");
        [[UIApplication sharedApplication] endBackgroundTask:[[DataCoordinator sharedInstance] backgroundTask]];
        [[DataCoordinator sharedInstance] setBackgroundTask:UIBackgroundTaskInvalid];
    }]];

The strange thing happening here is that, after the time provided by iOS for my application to run in background is over still the upload doesn't stops. Suppose I tried uploading 33 images, and when 179 seconds got complete application was successful uploading only 9 out of 33 images. But the process doesn't stop. And application keep uploading the remaining pictures.

4

1 回答 1

0

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

此属性包含应用程序在被系统强制终止之前必须在后台运行的时间量。

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

于 2015-04-20T07:45:20.617 回答