1

I know there is a lot of questions about running timer in background, for instance this one : how to keep running NSTimer when app goes in background , whose answer I will certainly use if I get nothing else, but my problem is not really when the application goes in background (that is, if I understand correctly what that means).

So here it is, I have a view controller with a timer that I use as a countdown, from this view controller it is possible to see videos. I use AVPlayerViewController for that part. :

AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];

Therefore I have a AVPlayerViewController above the one owning the timer and the timer stops counting until I leave this AVPlayerViewController. I would like it to keep down counting, is there a way to do that without writing current time in a file and so on ?

Thanks for your help

Community
  • 1
  • 1
Hugues Duvillier
  • 487
  • 6
  • 12
  • 1
    Are you invalidating timer in viewWillDisappear or any such methods ? – Midhun MP Nov 10 '15 at 09:23
  • Now I feel stupid, yes I invalidate it in viewWillDisappear. But the thing is that when I come back from the video, it still works, it just didn't change – Hugues Duvillier Nov 10 '15 at 09:26
  • Forget my last comment, I also start it in viewWillAppear, so no wonder it works when I come back. Thanks for your help, if you are willing to put your comment as an answer, I will accept it. – Hugues Duvillier Nov 10 '15 at 09:30

1 Answers1

2

There can be several reasons for such a behaviour, but a common thing I've noticed (saw several times in my career) is invalidating the timer in the viewWillDisappear or viewDidDisappear methods and starting it on the viewWillAppear and viewDidAppear methods. If you want to run a timer until the view controller is dismissed, then you should start the timer in the viewDidLoad method and invalidate it before you dismiss the view controller (or when you get the desired result, but never try to invalidate the timer in the dealloc method)

Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200