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
429 views
in Technique[技术] by (71.8m points)

ios - NSURLSession background download - resume over network failure

After reading the Apple documentation about the background download with the new iOS7 api (NSURLSession), I'm a bit disappointed. I was sure that Apple was managing the pause/resume over the network availability in the background (or provide an option to do so) but no…

So reading the documentation, this is what we've got:

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html

When any task completes, the NSURLSession object calls the delegate’s URLSession:task:didCompleteWithError: method with either an error object, or nil if the task completed successfully. If the task is a resumable download task, the NSError object’s userInfo dictionary contains a value for the NSURLSessionDownloadTaskResumeData key. Your app should use reachability APIs to determine when to retry, and should then call downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: to create a new download task to continue that download. Go to step 3 (creating and resuming task objects).

So far I understand the solution, but my question is: What architecture is the best to handle the loss of the network and resume downloading in the background?

On my side I'm using reachability and each time the network is available, I resume all tasks (referenced over a NSArray when creating), and suspends them when network is lost. This works well in foreground but for the background I need help on the following points:

  • If my app has no connectivity in foreground, if I go to the background without connectivity all my tasks remains suspended and won't came back if network is available…

  • Losing network in background, stop all my downloads/tasks. Scenario:

    • In foreground, I start downloading my tasks
    • I go to background and after 10s switch to "aireplan mode"
    • All my tasks got an error. So in the method URLSession:task:didCompleteWithError: I resume them using downloadTaskWithResumeData or if I can't (because some have not enough resume data) I'm creating a new task without resume-ing it (except if network is back at that time).
    • Then I put the wifi up
    • As I'm still in background I cannot trigger a "resume" when network is back without launching the application…

How do I address these points? Have I missed something?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

As I'm still in background I cannot trigger a "resume" when network is back without launching the application…


you can use "background fetch",when the app is launched by fetch,then you can check network and resume the download task.


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

...