Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

upload - NSURLSession: uploading assets with background transfer

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...