4

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?

Community
  • 1
  • 1
krusty
  • 51
  • 1
  • 7
  • I have prolonged the background task by not invalidating task when `beginBackgroundTaskWithExpirationHandler:` is called. I invalidate task only when download is complete and this works when device is locked manually. I have edited my question and added the code. But when device is automatically locked, app crashes after 10 min . Can anybody help? – krusty Apr 25 '12 at 15:59
  • Hi krusty. Did u try using GCD at all? It should fill up your need. – Selvin May 02 '12 at 11:56

2 Answers2

0

Only way I found this is possible:

  1. Use resumable transfers, download/upload in chunks.
  2. When application is sent to background continue transfer for 10 minutes
  3. Cleanup when time is up
  4. 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

lietus
  • 199
  • 2
  • 11
0
  1. If it is not necessary then pause it. It is the best thing that you can do.
  2. Else when your expiration handler is called then you can start again background task and you will get 10 mins more.
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184