1

I am trying to download file from my App and I have a Question About it what happens if phone has less memory than file size that is being downloading..

Is It trigger below urlSession delegate ? If it is what is the error?

 public func urlSession(_ session: URLSession, task: URLSessionTask, 
   didCompleteWithError error: Error?) {
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
tp2376
  • 690
  • 1
  • 7
  • 23

1 Answers1

2

According to NSURLSessionDownloadTask issues with Storage almost full disk warnings and https://forums.developer.apple.com/thread/43263 it would seem that you will get an error and its domain will be NSPOSIXErrorDomain with an error code of ENOSPC (Error, No Space).

There is also the possibility of getting an error with the domain of NSCocoaErrorDomain and an error code of 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
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I am passing (error?.localizedDescription)! to Alert .. So what ever error comes it will show on Alert right ..? – tp2376 Jul 13 '18 at 16:52
  • Yes, if you want to show the error to a user, using `localizedDescription` is what you need. But properly unwrap the optional. Don't use `!`. – rmaddy Jul 13 '18 at 17:19