1

I'm creating an AVAssetDownloadURLSession

here's my code:

let config = URLSessionConfiguration.background(withIdentifier: "background")
let avSession = AVAssetDownloadURLSession(configuration: config, assetDownloadDelegate: self, delegateQueue: nil)

let any : Any = avSession
print("any as? AVAssetDownloadURLSession \(any as? AVAssetDownloadURLSession)") // always returns nil        

Now, if I print out avSession, it says it is an __NSURLBackgroundSession, not an AVAssetDownloadURLSession. If I assign avSession to another variable (in this case an Any), and then downcast it later, it fails.

This is a real problem because I do need to do exactly this. Why can't I downcast this without failing? If I inspect the memory address of avSession, and then inspect any in the debugger, they both have the same address. But it is most certainly (according to the debugger, and the runtime itself) not an AVAssetDownloadURLSession.

Thanks for any help with this.

Dan Morrow
  • 4,433
  • 2
  • 31
  • 45
  • Could you explain why you would need the cast? Just to figure out if another solution might be possible. – Wolfgang Schreurs Dec 11 '16 at 02:30
  • After I create the `AVAssetDownloadURLSession`, I need to pass it off to another library, and this library doesn't know anything about `AVAssetDownloadURLSession`. At a later time when I need to use it, and I try and downcast the value, it fails. – Dan Morrow Dec 11 '16 at 04:07
  • And why would you not just use a variable of the right type? I.e.: why do you choose to assign to a variable of the type `Any` instead of `AVAssetDownloadURLSession`? – Wolfgang Schreurs Dec 11 '16 at 09:44
  • Sorry, I tried to keep the code small to demonstrate the odd behavior. But I didn't really explain the problem too well, apologies for that. What's happening is that I am creating the object just fine. But I need do something with it later, not right away. I'm using a 3rd party library that doesn't know anything about `AVAssetDownloadURLSession`, but I need to pass this newly created object to it, so that I block gets executed later. It's at that point, in that block, when I try and downcast this object to `AVAssetDownloadURLSession` where things go bad. The code above just demonstrates it. – Dan Morrow Dec 11 '16 at 17:34
  • As far as I can see it is not possible to downcast the __NSURLBackgroundSession to AVAssetDownloadURLSession. I've tried it many different ways, but no success. So I think if you really want to create the object first and later re-use, perhaps instead of storing the object, you could store the parameters needed to create the object. – Wolfgang Schreurs Dec 12 '16 at 18:46
  • I suggest filing this as a bug with Apple. – Tom Harrington Dec 12 '16 at 20:54
  • Thanks. filed. http://www.openradar.me/radar?id=5029435040333824 – Dan Morrow Dec 12 '16 at 21:08

0 Answers0