I have one timer that will countdown the time to 0. timer stop fine when it reach to 0 and when clicking submit button but timer doesn't stop when go out the application and come back.
I tried to create the timer in viewWillAppear and invalidate it in viewWillDisappear but timer still running after coming back from background. How to stop timer when enter to background? Any sample?
Thanks
You can use NSNotificationCenter. Add observer for UIApplicationDidEnterBackgroundNotification and stop your timer in it. You won't have to create your timer in AppDelegate in this case.
You can use the :
- (void)applicationDidEnterBackground:(UIApplication *)application
method of appDelegate class to stop your time . But for that you have to declare the timer inside the appDelegate class. Also you have to persist the counter when you will go to background .
Then you can use the viewWillAppear:
method to restart the timer.
Related
How can I use an NSTimer and applicationDidEnterBackground method at background for calling locationManager function?
Why would you need a timer? If you set your delegate to receive the callbacks from location updates, you won't need a timer. If you need active location tracking, you will be taking a huge battery hit if it runs for extended periods of time. You would be better off when you enter background to switch to -monitorForSignificantLocationChanges instead. Register your AppDelegate as as the Location Manager Delegate and just do what you need to do from the call backs.
In my application, I have pushed from oneviewcontroller to anotherviewcontroller. In that page I have started a timer and updated UITableview for every 1 sec (using UITableview reloaddata).
I have start the timer in viewwillappear function and invalidate timer in viewwilldisappear.
But when timer is running, when I came back to my viewcontroller my application crashed rapidly.
Please help me? Thanks in advance...
My guess is when i push back to viewcontroller that time tablereloads in previous viewcontroller that time it make crashes. I am using try catch but nothing makes any difference. Can we stop tableview reloads when we are not in that page?
The main reason of your app crashing is that, when-ever u come back form anotherViewController to oneViewController, your table view got release in between your timer still alive which is trying to update tableView and when you accessing the release object then app will get crashed. So u need to before stop your timer properly and nil it if timer isValid.
I hope mine answer will help you.
You strongly need to send invalidate message to all you timers at dealloc method of your class
- (void)dealloc
{
if ([_someTimer isValid]) [_someTimer invalidate];
}
I have a simple countdown timer that updates a label every second. How do I keep state or the illusion of it when hitting the home button or when the app gets put in the background?
Actually, you don't need to run in the background if all you need to do is maintain a timer. In your app delegate's applicationWillTerminate:, create an NSDictionary containing the NSTimer's fire time and write it to a plist using -[NSDictionary writeToFile:atomically:], then read it back in using -[NSDictionary initWithContentsOfFile:] somewhere in your app delegate'sapplication:didFinishLaunchingWithOptions:.
If you are running in the background anyway, do the same in applicationDidEnterBackground: and applicationWillEnterForeground:. If you use this solution, be sure to invalidate the timer after you write the plist.
If I have an NSTimer that starts in viewdidload, where is the proper place to invalidate it when leaving that view? Is it also necessary to release it as well?
If you create a timer with NSTimer scheduledTimerWithTimeInterval... then you don't need to release it, as you don't own it.
Now important thing is that the timer class retains the target, and in most cases we use self as the target. If the timer is not repetitive , then after the timer handler is completed, it automatically becomes invalid and the target is released. So you have nothing to do. But if your timer is still pending when leaving the view (this happens if you leave before the timer is fired or the timer is repetitive) then you need to invalidate it. The place MUST NOT be the dealloc method. As the timer itself retains the target, dealloc won't be called until the timer is invalid.
So it's better to invalidate when you know that you no longer need this. This might be the action which moves to the other view. Say user taps a button and in the button handler you move to other view. You can invalidate in this button handler.
I have an app with some animations which are called by timers. When one animation finishes then a timer is called to start another. If the iPhone sleeps and requires unlocking to activate it then the animations do not start. This is obviously because the timer ends whilst the app is asleep and then the function to restart an animation doesn't get called.
What I want to do is check if the animations are running and if not activate them - or maybe stop them when the app sleeps and activate them when it starts again.
I have been trying to find a method to do this but after hours of googling I can't find anything - it is completely possible I'm not searching for the right thing!
Any suggestions would be greatly appreciated?
Also is there a way of forcing the iPhone to sleep when connected to XCode?
THANKS
When the iPhone screen locks the notification UIApplicationWillResignActiveNotification is sent. After the screen is unlocked the notification UIApplicationDidBecomeActiveNotification is sent. You have two choices:
You can register with NSNotificationManager to get UIApplicationDidBecomeActiveNotification notification and restart your timers after wakeup if they are invalidated.
You can implement applicationDidBecomeActive in your app delegate and restart your timers there if they are invalidated.