background timer

hi, i need to run a timer every 5 minutes during app is in background for check data from a webserver... after some minute the timer not called... what's is the problem?



dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


t = [NSTimer scheduledTimerWithTimeInterval:mainDelegate.TimerAppySec target:self selector:@selector(timerSuono:) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];

[[NSRunLoop currentRunLoop] run];

});

Replies

The reason why your timer doesn’t fire is that your app is suspended shortly after moving to the background. In order to support an important user goal, all day battery life, iOS puts strict limits on how apps can execute in the background. There are a variety of special purpose mechanism for running in the background, but they are all tied to specific user cases.

i need to run a timer every 5 minutes during app is in background for check data from a webserver

iOS has no general-purpose run-me-every-N-minutes mechanism. The recommended way to address your specific problem is via push notifications. How you implement this depends on whether you control this web server:

  • If you control this web server, you can have it act as a push provider, issuing push notifications when relevant content changes.

  • If you don’t control this web server, you will have to deploy your own push provider. This would poll the the original web server and issue push notifications if the relevant content changes.

    The advantage of this approach is that the energy cost of polling for content is paid by a server that’s connected to mains power, not by a bunch of battery-powered iOS devices.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

but app like spotify works during background, what is the difference?

but [music playing apps] work during background, what is the difference?

iOS has affordances for certain types of apps, allowing those apps to run in the background under specific user scenarios. Music playing apps are one such example. Check out the Background Execution section of the App Programming Guide for iOS.

These are not, however, general-purpose background execution facilities. Each of these background modes was designed for a specific user scenario and only works well in that scenario. For example, a turn-by-turn navigation app can run indefinitely in the background but only if navigation was started while the app was in the foreground.

In addition, App Review pays very close attention to the background modes each app uses, requiring that their use be aligned with user expectations (see clause 2.5.4 of the App Store Review Guidelines).

The scenario you’ve described, polling a web service every 5 minutes, is very unlikely to match any of these background modes. The only one that might seem like a good match is the

fetch
background mode, but that’s not going to work for you (see this post for some context). Which brings me back to the recommendations I made in my previous post.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Is it at all plausible to utilize/enable push notifications in iOS without attaining Apple Developer Program Membership?

Is it at all plausible to utilize/enable push notifications in iOS without attaining Apple Developer Program Membership?

No. To send a push notification your push provider needs credentials, and those credentials can only be allocated via the developer web site.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Once application in the background how soon iOS will stop processing any activity in the background?


What I mean I run a task and put the application in the background will it instantly stop all execution or it will give a minute or two before suspending it?

Once application in the background how soon iOS will stop processing any activity in the background?

That is unspecified, and it also depends on the context. In most cases the system will suspend an app a few seconds after it becomes eligible for suspension. An app is eligible for suspension if it’s in the background and there’s nothing preventing it from being suspended. There are various things that can keep an app from being suspended, the most common being:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"