0

I am testing out a NSTimer with selector in sceneDidEnterBackground, but the selector does not run when I press the home button once. I am trying to run a timer when the home button is pressed once. Does anyone know what is wrong? This is my code in SceneDelegate.m

- (void)sceneDidEnterBackground:(UIScene *)scene {
    NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0
                          target: self
                          selector:@selector(test)
                          userInfo: nil repeats:NO];
//    NSLog(@"test"); //it prints out "test" here
    [(AppDelegate *)UIApplication.sharedApplication.delegate saveContext];
}

- (void) test{
    NSLog(@"test");
}
Beck
  • 5
  • 3
  • 1
    Timers do not run in the background so exact what's the plan here? – matt Jun 24 '21 at 05:44
  • I am trying to do some session management thing so I am trying to run a function in the background. Like when the app has been in the background for a minute, it will call another function – Beck Jun 24 '21 at 05:57
  • When the app has been in the background for a minute it will be totally suspended and none of its code can run at all. It will not be calling _any_ functions. – matt Jun 24 '21 at 06:34
  • Then is there a way to run any timer when the home button is pressed once? I've been trying out BGTaskScheduler but it is not working as well – Beck Jun 24 '21 at 07:34
  • There is a way: you need to be playing music with the background capability. – matt Jun 24 '21 at 07:38

0 Answers0