0

Team,

i'm developing an iOS application. My requirement is to query for specific news service(REST API) in regular time interval.I wanted query the service twice for a day and update my sqllite db, even the applciation is in background state. My UI will be updated with data fetched from sqllite db, while the application is in foreground.

My question are,

  1. Is it possible to run NSTimer in background continuously? if yes, is there any maximum time limit for timer to run in background (say 10 mins or 60 mins)?

  2. Is it possible to send request to download a file using NSUrlConnection and save the file to documents directory, when the application is in background ?

Your suggestions will be much helpful for my project design. Thanks in advance.

shatthi
  • 656
  • 3
  • 10
  • 23

2 Answers2

0

Answering my own question, so that it will be helpful for others.

Ques 1: Is it possible to run NSTimer in background continuously?

Ans: Nstimer will not run while the application in background state. So there is no point of maximum allowed timer value in background. If the application enters into background while there is an ongoing process, [UIApplication beginBackgroundTaskWithExpirationHandler:] can be used to complete the ongoing process. The maximum time allowed by the OS with this handler is 10mins.

Ques 2: Is it possible to send request to download a file using NSUrlConnection and save the file to documents directory, when the application is in background ?

Ans: Below given information is from Apple documentation. Detail info is found here

In iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background,such as a music player app
  • Apps that keep users informed of their location at all times, such as a navigation app
  • Apps that supportVoice over Internet Protocol (VoIP)
  • Newsstand apps that need to download and process new content
  • Apps that receive regular updates from external accessories

Info about running background process using VOiP type application can be found here

Community
  • 1
  • 1
shatthi
  • 656
  • 3
  • 10
  • 23
0

What you are aiming for cannot be achieved on iOS:

Arbitrary apps cannot run in the background for an arbitrary amount of time.

You can try to mitigate some of this by using local notifications instead of NSTimer to schedule your updating. This will, however, only buy you a very limited amount of time to do your networking.

The question you should ask yourself at this point probably is:
If you are only updating twice a day, how bad can it be to initiate the download when your app becomes active?

danyowdee
  • 4,658
  • 2
  • 20
  • 35
  • thanks for ur comments, i would like to atleast complete the ongoing current download when the application enters into background in middle of download. – shatthi Mar 14 '12 at 05:35
  • That is (to a certain extent) possible: see https://developer.apple.com/library/ios/technotes/tn2277/ and the documentation for `-[UIApplication beginBackgroundTaskWithExpirationHandler:]` (somehow the link to the method breaks when posted here...) for reference. – danyowdee Mar 14 '12 at 17:36