NSURLSessionDownloadTask与存储将满的磁盘警告有关的问题 [英] NSURLSessionDownloadTask issues with Storage almost full disk warnings

查看:208
本文介绍了NSURLSessionDownloadTask与存储将满的磁盘警告有关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSURLSessionDownloadTask处理ios上的空间不足"/全盘"错误时遇到了问题

I'm having issues with handling "out of space" / "full disk" errors on ios with NSURLSessionDownloadTask

如果由于在应用程序中完成下载而导致磁盘已满,我会打电话给

If the disk is full due to downloads done in the app I get a call to

URLSession(会话:NSURLSession,任务:NSURLSessionTask,didCompleteWithError错误:NSError?)

URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?)

具有域NSPOSIXErrorDomain的错误和错误代码ENOSPC

with the error having domain NSPOSIXErrorDomain and error code ENOSPC

但这只会对一项任务发生一次,对所有正在运行的情况不会发生

But this only happens once for a task, not for all running

例如:如果我当时有3个任务在运行,那么我只能为其中一个任务执行此任务,而其他2个任务仍处于正在运行"状态 它们不接收任何字节,但是也不会失败. 此外,在所有这些任务上调用cancel会将其状态从正在运行"更改为取消",并且它们将无限期保持这种状态.

Ex: If I have 3 tasks running at the time, I only get this for one of them and the other 2 remain in the state Running They don't receive any bytes, but they don't fail either. Moreovever, calling cancel on any of those tasks changes their state from Running to Cancelling and they remain like this indefinitely.

我的解决方案是,当我第一次收到此错误时,为会话调用invalidateAndCancel并处理所有正在运行的任务的失败. 当完整磁盘"是由应用程序的下载引起的时,这似乎起作用. 但是,如果全盘"错误是由外部下载引起的(例如:iTunes文件共享,Podcast下载,其他应用程序下载),则我在应用程序中没有收到任何错误 我所有的下载任务都保留为正在运行"或正在取消"(如果我尝试取消它们) 他们不会下载任何内容,也不会因NSURLSessionDelegate或NSURLSessionDownloadDelegate的任何回调而失败

My solution was, when I receive this error the first time, to call invalidateAndCancel for the session and handle failure for all running tasks. This seems to work when the "full disk" is caused by the downloads made by the app. But if "full disk" error is caused by external downloads (Ex: iTunes file sharing, downloading Podcasts, other apps downloading) I receive no error in the app All my download tasks remain in Running, or Cancelling (if I try to cancel them) They don't download anything, they don't fail with any of the callbacks for NSURLSessionDelegate or NSURLSessionDownloadDelegate

开始下载之前,我检查了设备上的可用空间 我还考虑了当前正在运行的任务 但是我无法控制设备上的其他下载,这些下载最终可能会触发已满磁盘"警告

Before starting a download, I check the available space available on device I also take into consideration the currently Running tasks But I have no control over other downloads on the device that might end up triggering the "full disk" warning

如何处理这些情况? 尽管下载任务不再下载,它们是否仍将保持运行状态? 我是否应该为每个任务或至少对于会话使用(NSPOSIXErrorDomain,ENOSPC)进行didCompleteWithError调用? 或者至少我不应该能够成功取消它们?并获得didCompleteWithError调用吗?

How can I handle these cases? Are the download tasks expected to remain in Running state though they are not downloading anymore? Shouldn't I get a didCompleteWithError call with (NSPOSIXErrorDomain, ENOSPC) for each task, or for the session at least? Or at least shouldn't I be able to successfully cancel them? and get a didCompleteWithError call anyway?

我是否缺少一个委托电话,那会让我知道现在该关闭所有正在运行的任务了?

Is there a delegate call I'm missing, one that would let me know it's time to close all running tasks?

我正在使用共享后台会话执行更多后台下载任务

I'm using a shared background session for more background download tasks

下载任务是使用NSURLSession创建的: func downloadTaskWithRequest(_ request:NSURLRequest)-> NSURLSessionDownloadTask

The download tasks are created with NSURLSession's: func downloadTaskWithRequest(_ request: NSURLRequest) -> NSURLSessionDownloadTask

sesion配置是使用NSURLSessionConfiguration的创建的: class func backgroundSessionConfigurationWithIdentifier(_ identifier:String)-> NSURLSessionConfiguration

The sesion configuration is created using NSURLSessionConfiguration's: class func backgroundSessionConfigurationWithIdentifier(_ identifier: String) -> NSURLSessionConfiguration

使用NSURLSession的创建该种子 init(配置配置:NSURLSessionConfiguration, 委托代表:NSURLSessionDelegate ?, proxyQueue队列:NSOperationQueue?)

The seesion is created using, NSURLSession's init(configuration configuration: NSURLSessionConfiguration, delegate delegate: NSURLSessionDelegate?, delegateQueue queue: NSOperationQueue?)

我正在使用maxConcurrentOperationCount为3的NSOperationQueue 我已经实现了我的NSURLSessionDelegate和NSURLSessionDownloadDelegate

I'm using a NSOperationQueue with a maxConcurrentOperationCount of 3 I have implemented my NSURLSessionDelegate and NSURLSessionDownloadDelegate

任务在前台和后台运行都很好.

Tasks seem to run fine in foreground and background.

谢谢

Ps:使用Xocde7,ios9 sdk,并在ios9设备上进行了测试

Ps: using Xocde7, ios9 sdk, tested on an ios9 device

推荐答案

我还问过苹果有关此问题的信息,答案回来了:

I had also asked Apple about this issue and the answer came back:

似乎是因为后台会话使用磁盘来保持其状态,所以当磁盘已满时的行为不能完全可靠地返回ENOSPC

Seems like, because the background session uses the disk to persist its state, behaviour when the disk is full is not exactly reliable to return the ENOSPC

现在,开发人员有责任处理完整磁盘盒:

And the handling of full disk case is the developer's responsibility for now by:

  • 在开始下载之前检查磁盘空间

  • checking for disk space before you start a download

在下载应用程序时监视磁盘空间-如果您的应用程序位于前台,或者您的应用程序在后台恢复(或重新启动),请主动监视可用的磁盘空间.如果它低于某个阈值,请暂停下载(通过调用-cancelByProducingResumeData :,以便稍后再恢复).

monitor disk space when your app is downloading — If your app is in the foreground, or your app gets resumed (or relaunched) in the background, actively monitor the disk space available. If it’s below some threshold, suspend your downloads (by calling -cancelByProducingResumeData:, so you can resume them later on).

有关详细的答案检查,请执行以下操作: https://forums.developer.apple.com/thread/43263

For a detailed answer check: https://forums.developer.apple.com/thread/43263

这篇关于NSURLSessionDownloadTask与存储将满的磁盘警告有关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆