How do I keep a block of code in the background?

I have searched this thread, mostly I come across Apple limitations. I need to manage my server once a minute and fire a local notification if there are changes. I need how to keep the timer (NSTimer) in the background (or when the device lock is activated.) Any idea please. Thanks to

+3


source to share


3 answers


You need to redesign your application. You cannot guarantee that your application will never get killed when the OS exits the game to free up memory. What happens in this scenario? Push Notifications are your best bet here. First of all, you don't need to query the server every 60 seconds; you simply fire a notification when the content you are interested in changes on the server. Secondly, the notification will be received even if your application is not running.



Another problem is that you have to tell Apple through your info.plist which backgrounds your application supports. This is valid for applications that run music or VoIP in the background. Web server polling is not one of the supported modes. With push notifications, you also get some delegate methods that you can use to process the information sent through the notification when the app comes to the fore.

+4


source


You can execute your logic in the server part, and if there are changes, send a Push Notification.



+5


source


My app is constantly running in the background with the following piece of code .....

-(void)applicationDidEnterBackground:(UIApplication *)application

{ 
  UIApplication *app = [UIApplication sharedApplication];

  UIBackgroundTaskIdentifier bgTask = 0;

  backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self   selector:@selector(backgroundTask) userInfo:nil repeats:YES];

  bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
  }];

}

      

Now do whatever you want in backgroundTask .

+2


source







All Articles