由于 phone 中内存不足,下载文件失败时出现 URLSession 错误

URLSession Error when Downloading file failed because of less memory In phone

我正在尝试从我的应用程序下载文件,但我对此有疑问如果 phone 的内存小于正在下载的文件大小,会发生什么情况..

它是否在 urlSession 委托下方触发?如果是错误是什么?

 public func urlSession(_ session: URLSession, task: URLSessionTask, 
   didCompleteWithError error: Error?) {
}

根据 NSURLSessionDownloadTask issues with Storage almost full disk warnings and https://forums.developer.apple.com/thread/43263 看来您会收到一个错误,其域为 NSPOSIXErrorDomain,错误代码为 ENOSPC(错误,无 Space ).

也有可能出现域NSCocoaErrorDomain错误,错误代码NSFileWriteOutOfSpaceError

public func urlSession(_ session: URLSession, task: URLSessionTask,  didCompleteWithError error: Error?) {
    if let nserror = error as? NSError {
        if (nserror.domain == NSPOSIXErrorDomain && nserror.code == ENOSPC) ||
           (nserror.domain == NSCocoaErrorDomain && nserror.code == NSFileWriteOutOfSpaceError) {
            // Not enough space
        }
    }
}