2

My app run in background and uses NSTimer to launch audio after a certain amount of time. Me and my testers have no problems with this, and audio can be launched event after several hours in the background (>10h).

My issue is that some users reports that the audio is often delayed, sometimes by a few minutes, sometimes by an hour.

I do something like that:

UIApplication * app = [UIApplication sharedApplication];
  bgTask = [app beginBackgroundTaskWithExpirationHandler:nil];

dispatch_async(dispatch_get_main_queue(), ^{
    theTimer = [NSTimer scheduledTimerWithTimeInterval:[theDateIWant timeIntervalSinceNow] target:self selector:@selector(playAudio) userInfo:nil repeats:NO];
    });

My questions are :

-Does a NSTimer can be delayed that much by the system ? if so how to go around this problem ? -How to reproduce this kind of issues ? - Is it safer to use the following ?

theTimer = [[NSTimer alloc] initWithFireDate:theDateIWant interval:0.0 target:self selector:@selector(playAudio) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:theTimer
                          forMode:NSDefaultRunLoopMode];

Thanks for any help !

OthmanT
  • 223
  • 3
  • 13

1 Answers1

1

It seems to me that you understand background tasks a little bit wrong.

When the first snippet is performed (I assume that you place it in the applicationDidEnterBackground method), it says the system

I have a small task to perform, please wait for a while till i finish it!

and the system will wait for about 5-10 minutes before suspend the application.

If you need kind of alarm, you can use LocalNotification to playback specific sound at specified time.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • Thanks for the answer, but as I said, my system already works and my users expect to hear audio event in silent mode. And I already use notifications as a secondary system. I am more concerned about delays – OthmanT Mar 29 '17 at 18:22
  • Actually, the timeout on current iOS versions is exactly 3 minutes. – Sulthan Mar 29 '17 at 20:29
  • Yes but I get around it with a loop that plays audio every 2 minutes-ish, that gives me an all new 3 minutes to run wathever – OthmanT Mar 29 '17 at 21:49
  • @OthmanT Sounds like a terrible app design, waiting to be rejected from the AppStore. – Léo Natan Apr 03 '17 at 20:35
  • @LeoNatan, you'd be surprised to know how many alarm apps uses this on the app store, there is no other choice really. But that's not really my question. Thanks anyway – OthmanT Apr 03 '17 at 20:37
  • @LeoNatan Can a pushNotification trigger some audio from background even on silent ? – OthmanT Apr 03 '17 at 20:40
  • You get runtime in the background. How is that different than background runtime after silent audio playback? – Léo Natan Apr 03 '17 at 20:41
  • @LeoNatan this runtime can't wake up your app and play audio with an AVAudioPlayer – OthmanT Apr 03 '17 at 20:46
  • @OthmanT "In order for AVAudioPlayer to start playing while the application is in background, your app must support remote control events." http://sapandiwakar.in/switch-to-a-new-song-in-background-for-ios/ – Léo Natan Apr 03 '17 at 20:49
  • @LeoNatan, thanks, this is interesting! But I still have to manage without having to setup a notification server. Can this be achieved with a local notification ? – OthmanT Apr 03 '17 at 20:52
  • No. Local notifications do not allow background execution time. – Léo Natan Apr 03 '17 at 20:54
  • @LeoNatan, would you be available to discuss some of this issue via mail or something ? that would help me a lot arrange some of my architecture flaws – OthmanT Apr 03 '17 at 21:14
  • Sure, you can find my contact information in my profile. – Léo Natan Apr 03 '17 at 21:15