NSURLSessionDownloadTask not deleting the file when the app is closed by the user and the task was still active

时间:2015-07-28 16:49:29

标签: ios xcode nsurlsession nsurlsessiondownloadtask

I have a NSURLSession and a NSURLSessionDownloadTask configured for downloading a file in background, if the download task in canceled by the user all the data is deleted and the storage space the file was using is freed, but if the app is closed from the multitasking dock the download task is terminated and gives an error but is not deleting the data and the temporal data for the file is still occupying storage space and is never freed. What do i need to do in order to free the space ?

This is my NSURLSession configuration and error handling:

- (NSURLSession *)backgroundSession {
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        NSURLSessionConfiguration *configuration;
        if ([[UIDevice currentDevice].systemVersion hasPrefix:@"7"]) configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.visyon.pr"];
        else configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.visyon.pr"];
        configuration.sessionSendsLaunchEvents =YES;

        session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

    });
    return session; }


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {


if (error == nil) {
    NSLog(@"Task: %@ completed successfully", task );
} else {
   // [self hideActivity];
   // [self showAlertBoxErrorDownload];
    NSLog(@"Task: %@ completed with error: %@, %lu", task, [error localizedDescription], (long)error.code);

}    self.downloadTask = nil; }

3 个答案:

答案 0 :(得分:4)

好的,经过10天以上的尝试和失败我找到了解决方案,首先有两种情况当用户关闭应用程序并且在后台有活动下载时,请考虑到这种情况下载任务永远不会被恢复。

场景1.当用户关闭应用程序但仍处于活动状态时,将调用- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error,并且用于下载的临时文件将直接转到应用程序的临时目录,在本例中为{调用{1}}但首先调用下载任务的委托。解决方案是这种情况是每次打开应用程序时实现代码来清理临时目录,或者让iOS清理临时文件夹,这里解释当iOS要清理临时文件时:

When does iOS clean the local app ./tmp directories?

方案2.当用户在后台模式下关闭应用程序时,应用程序将被终止,下次打开应用程序时,将调用- (void)applicationWillTerminate:(UIApplication *)application,并且用于下载的临时文件将存储在{ {1}}在下一个目录中:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

下次有新的下载时,临时文件将移动到应用程序的临时目录。

此处的解决方案是实现代码,以便在NSCachesDirectory启动后立即从var/mobile/Containers/Data/Application/CA21-B6E8-3305A39/Library/Caches/com.apple.nsurlsessiond/Downloads/com.xxx.xxx/CFNetworkDownload_M5o8Su.tmp删除所有时态文件。

以下是从Caches/com.apple.nsurlsessiond/Downloads/com.xxx.xxx/删除时态文件的代码:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

但是因为它只适用于这种情况,所以每次启动时都可以更好地实现代码来清理应用程序的临时目录,或让iOS清理临时文件夹,以便它适用于这两种情况。

以下是有关如何清理应用的临时目录的代码:

NSCachesDirectory

答案 1 :(得分:0)

First point is if you are configure nsurlsession with background configuration type. Then this type of session till runing if user kill the app. Second point if you want to clear space then you need to stop that session and manually write code for deleting temporary file from tmp folder.

Change session configuration type.

答案 2 :(得分:0)

AppDelegate中有一个名为- (void)applicationWillTerminate:(UIApplication *)application的方法。您可以尝试从那里调用[NSURLSessionDownloadTask cancel];;或者尝试设置一个标记,以便在应用程序关闭时向应用程序指示正在进行下载,并在下次打开应用程序时删除数据。