问题
I have a requirement where user can download multiple files one after other. When my app goes in background OR when iPad is locked, the download or web-service response can be get only for 10 minutes.
My download happens in a separate thread, I have implemented beginBackgroundTaskWithExpirationHandler: after some googling and on stackoverflow links.
How to implement Task completion
App crash because of auto lock in iphone?
and iOS documetation
https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Now some of my selected files are downloaded and some failed because one can run background task only for 10 min max.
Is there any alternative for this?? Should I pause download completely while app goes in background?? Can anyone help me regarding this??
I have succeeded to do so by not ending background task if my download is in progress
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"\n beginBackgroundTaskWithExpirationHandler called \n");
if(![self checkIfDownloadInProgress]){
[self endTaskOnCompletion];
}
}];
This works only if device is manually locked by user. If device locks automatically after 2 min, app runs for 10 min and then it is crashed Can anybody help?
回答1:
- If it is not necessary then pause it. It is the best thing that you can do.
- Else when your expiration handler is called then you can start again background task and you will get 10 mins more.
回答2:
Only way I found this is possible:
- Use resumable transfers, download/upload in chunks.
- When application is sent to background continue transfer for 10 minutes
- Cleanup when time is up
- Resume transfer when application is activated
This document pretty much summarizes possible solutions in Networking And Multitasking: https://developer.apple.com/library/ios/#technotes/tn2277/_index.html
来源:https://stackoverflow.com/questions/9411122/download-large-files-which-takes-more-than-10-min-when-ipad-device-is-locked