3 Replies
      Latest reply on Jan 30, 2019 1:02 AM by eskimo
      Simone P. Level 1 Level 1 (0 points)

        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];

            });

        • Re: background timer
          eskimo Apple Staff Apple Staff (10,695 points)

          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"

            • Re: background timer
              Simone P. Level 1 Level 1 (0 points)

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

                • Re: background timer
                  eskimo Apple Staff Apple Staff (10,695 points)

                  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"