What's the best approach to use background transfer to upload assets from gallery?
Seems like uploadTaskWithRequest:fromData:
doesn't work with NSURLSession
created with backgroundSessionConfiguration
since it causes an exception: "Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks in background sessions must be from a file". Judging from the exception description background transfer shouldn't work with streamed upload tasks too.
Fair enough. uploadTaskWithRequest:fromFile:
is the first thing that comes to mind when you think about uploading data that you already have on the disk. But the NSURLSession Class Reference tells us that this method works only with file urls, but when you get asset's url path it starts from "assets-library://asset/..." and providing this path doesn't work too.
So it seems that the only option left is to copy file from the assets library into a temporary directory, and provide its file url to uploadTaskWithRequest:fromFile:
. But it doesn't make any sense because you already have asset file saved on your disk.
Am I missing something?
Update:
Talked with Apple engineer at Tech Talks event and he confirmed that background NSURLSession supports only file urls. So indeed, you need to copy asset library into a temporary directory, and provide its file url to uploadTaskWithRequest:fromFile:
to upload it with background NSURLSession.
This behavior may change in the future though.
They also say somewhere in documentation that
Upload/download urls support only file http https schemes. Background supports only http/https.
I believe that core data and assets library urls despite being technically valid urls aren't really urls, and most likely aren't implemented by creating custom NSurlprotocol-s.
So I am inclined to believe that you should write the asset to some file and pass a URL to that file to upload task. I'm working on a project that uploads assets as well and I have just begun converting it to nsursession using background session. So I'll try this out for myself later today to prove the theory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With