16

I am using NSURLSession to download some mp3 files and store them in the device. Everything works fine but sometimes out of the blue, the app crashes and i get this weird error saying NSURLErrorDomain Code=-997 "Lost connection to background transfer service". It does not happen alot but when it does it just messes up the whole app, like when i launch the app the next time all the download tasks are messed up and i just have to rebuild the application on the device to get it to work again. Note that i only get this error since using Xcode 6 and ios8, even though I'm not sure its is related to ios8 or not.

This is the complete error description:

Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service" UserInfo=0x178664100 {NSErrorFailingURLKey=http://XXXXXXXXXX.mp3, NSLocalizedDescription=Lost connection to background transfer service, NSErrorFailingURLStringKey=http://XXXXXXXXXXX.mp3}
Rashid
  • 762
  • 1
  • 9
  • 30
  • 1
    have you found a solution? i have the same problem – zaitsman Nov 18 '14 at 23:39
  • @zaitsman mine had to do something with the URL that the files are being saved to. Ever since i solved that problem i am not getting this error anymore! – Rashid Nov 22 '14 at 10:16
  • @jim Did you change the URL the files were being saved to in the delegate method implementation `URLSession: downloadTask: didFinishDownloadingToURL:` ? I'm encountering the same error, and I'm trying to determine _where_ I need to make the change – Mike Welsh Jan 19 '15 at 18:13
  • Are you doing this on the main thread? – Eddwin Paz Feb 02 '15 at 21:43
  • @jim I recommend you to read the following tutorial will give you more perspective on whats going on with your issue.. http://code.tutsplus.com/tutorials/ios-7-sdk-background-transfer-service--mobile-20595 – Eddwin Paz Feb 02 '15 at 21:51
  • @jim were you able to find a solution to this? – Syed Ismail Ahamed Apr 21 '16 at 08:18

4 Answers4

8

My error was that I used a dataTaskWithRequest instead of uploadtaskWithRequest or in your case downloadtaskWithRequest. Obviously a datatask can't run in the background and get's killed.

Caro
  • 511
  • 6
  • 16
  • 1
    What if you want to do a background upload that doesn't have a file associated with it? ie. just some JSON? You have to save it to a file first? – shim Jan 08 '16 at 20:30
  • Yes I solved it by saving the request body into a file. – shim May 25 '16 at 15:13
  • @shim, I am getting error when I upload a file which has contents of request body. can you share me some codes for it? – gstream Apr 20 '17 at 09:49
  • Sorry, I don't have access to that code anymore. Anyway, I would imagine any such error really depends on the server you're using. – shim Apr 20 '17 at 15:25
1

What version of the SDK are you on? There was a bug in the background transfer daemon that could cause it to crash, which has since been fixed. I would download the latest Xcode beta and try it again.

Alex Nauda
  • 4,126
  • 1
  • 24
  • 24
  • Well ever since i downloaded xcode 6 and started using ios8 i have not had a problem with background download! you are probably right. – Rashid Feb 05 '15 at 15:44
  • I don't think this is necessarily the same bug. I'm using the SDK 8.1 and running on iOS 8.1.3 and also getting occasionally the lost connection error. The funny thing is I get this right after the data has been downloaded successfully! I.e. first the callback URLSession:downloadTask:didFinishDownloadingToURL: with response data and everything OK and immediately after that the callback URLSession:task:didCompleteWithError: is invoked with the 'lost connection' error. I'll be reporting it in a radar. – Oscar Hierro Feb 06 '15 at 13:29
1

Probably Background Transfer daemon crashed. Try to check crash logs on your device. May be you will find something interesting there.

Skie
  • 1,942
  • 16
  • 27
0

I had this issue with AVAssetDownloadTask, and it turned out to be because I was creating tasks without starting them.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145