NSURLSession: uploading assets with background transfer

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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.

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!