NSTimer disables dealloc in UIView - objective-c

#interface someview:UIView{
NSTimer* timer;
}
#end
#implementation someview
-(void)dealloc{
NSLog(#"dealloc someview");
[timer invalidate];
timer = nil;
}
-(void)runTimer{
//
}
-(void)someMethod{
timer = [NSTimer timerWithTimeInterval:2.0f target:self selector:#selector(runTimer) userInfo:nil repeats:YES];
}
#end
Releasing someview will NOT call dealloc and the timer keeps on running.
If I comment out the "timer = [NSTimer schedule...." part, dealloc will be called. Which means all the other part of my code is working properly and the timer is the culprit. The runTimer method is empty, which means it's just the timer messing with me.

I think the best solution when using an NSTimer inside of a UIView is to override the removeFromSuperview method;
- (void)removeFromSuperview
{
[timer invalidate];
timer = nil;
[super removeFromSuperview];
}
The only thing to keep in mind here is that you need to ensure that timer is not a nil object because removeFromSuperview can also get automatically called from other UIView's super dealloc methods. You could wrap in a conditional to check.

NSTimer retains the target. Therefore, the timer must be invalidated before your view is dealloc'd.

As mentioned above, Timers retain their targets. Until the timer is invalidated, there is a retain cycle between the timer and the view, so the view will not be deallocated.
I would invalidate the timer when it's removed from the view hierarchy by subclassing didMoveToSuperview, this gets called by the system when there is a View-Related Change (e.g superview changes). The 'removeFromSuperview' is only called when removeFromSuperview is called on UIView
- (void)didMoveToSuperview
{
[super didMoveToSuperview];
if (!self.superview)
{
[timer invalidate];
timer = nil;
}
}

Related

Correct way to display current date/time in iOS app and keep it up to date

I'm wondering this primarily out of the theory/performance side of things, but if I needed to display the current date/time in a UILabel or something, and then keep it up to date (say, down to the minute), what is the most correct way to do so?
I've seen various examples online of "clock" apps that basically just have a recursive method that runs with a 1 second delay between invocations. Another option would be to create a repeatable timer and add it to the run loop. There are probably a variety of other ways to do this as well. Is there one way that is better than the others?
The recursive method is not a great idea.
Use NSTimer approach. Create the repeating timer in viewDidAppear and invalidate the timer in viewDidDisappear (to avoid retain cycle, a.k.a. strong reference cycle). For example, assuming you have a timer property:
#property (nonatomic, strong) NSTimer *timer;
You would then schedule and invalidate the timer as follows:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(handleTimer:) userInfo:nil repeats:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
- (void)handleTimer:(NSTimer *)timer
{
// update your label here
}
If you don't release and/or invalidate the timer, it will maintain a strong reference to your controller and you can end up with a strong reference cycle. And you cannot resolve this strong reference cycle in dealloc (because dealloc is not called until the controller has no more strong references; the presence of a repeating NSTimer that has not been invalidated will prevent dealloc from ever getting called), which is why we do it in viewDidDisappear.

NSTimer Not Stopping When Invalidated

I have the following code in my .h file:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AVFoundation/AVFoundation.h>
#import <MapKit/MapKit.h>
#interface LandingController : UIViewController<CLLocationManagerDelegate> {
CLLocationManager *LocationManager;
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) NSTimer *messageTimer;
- (IBAction)LoginButton:(id)sender;
#end
I have the following code in my .m file:
#interface LandingController ()
#end
#implementation LandingController
#synthesize messageTimer;
- (void)checkForMessages
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"BINGO:"
message:#"Bingo This Works"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)LoginButton:(id)sender {
if ([UserType isEqualToString:#"Owner"]) {
if (messageTimer){
[self.messageTimer invalidate];
self.messageTimer = nil;
}
} else {
if (!messageTimer){
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
}
}
}
#end
But my timer doesn't want to stop when I call the invalidate.
The LoginButton is only pressed twice, once when the strResult is = to "Guard" and then the application changes it to be equal to "Owner" and the user presses the login button again, so I don't think I'm setting multiple timers.
After pressing the login button and starting the timer I segue to another view and then segue back to press the login button once more which is when I want the timer to stop. Do I need to do anything special to get the messageTimer since I switched views for a moment and then came back?
Any ideas?
Thanks!
You need to call [self.messageTimer invalidate] on the same thread on which you created the timer. Just make sure that the timer is created and invalidated on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if ([UserType isEqualToString:#"Owner"]) {
[self.messageTimer invalidate];
self.messageTimer = nil;
} else {
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:YES];
}
});
NSTimer is retained by NSRunLoop, so the only way I see your issue happening is if you're actually creating more than one timer and invalidating only what you have reference to.
Example:
if(!timer)
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:#selector(processTimer) userInfo:nil repeats:YES];
Have you try to put repeat as No
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(checkForMessages)
userInfo:nil
repeats:NO];
If the code at the end (starting with if) is called twice with UserType != Owner, you create a new timer without invalidating the old one. Now two timers are running. If the code executes a third time, you will add a third timer and so on. Executing the code with UserType == Owner only invalidates the last timer and even it is called repeatly, it does not invalidate older timers.
You have a timer leak. ;-)
How about put an NSLog in your checkForMessages method? It would be easier to check if there's really only 1 timer.
(I'd rather put this in a comment, but I don't have that much reputation....)
I have an approach for stopping or deactivate the timer, Before apply this make sure that you tried all the cases as mentioned above so you can also understand that why this approach used at last.
// Instead of self use NSTimer class, it will not provide you any crash and in selector placed any empty function and setRepeats to NO
self.messageTimer = [NSTimer scheduledTimerWithTimeInterval:100.0
target:NSTimer selector:#selector(emptyFunctionCalling)
userInfo:nil
repeats:NO];
[self.messageTimer invalidate];
self.messageTimer = nil;
So whenever the case occured that timer will not stopping then entering in this code the timer will stops permanently.
Its weird but invalidating passed timer reference and created timer reference worked for me.
delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateDelayLable:) userInfo:nil repeats:YES];
-(void)updateSendDataDelayLable:(NSTimer*)timer{
delayValueForGNSS--;
if (delayValueForGNSSSend==0) {
[timer invalidate];
timer = nil;
[delayTimer invalidate];
delayTimer = nil;
}
}

NSTimer within a class

I have a timer class set up that is basically handling all of count down timer logic. All it does is on button press - counts from 60 to 0 seconds.
I have the following code in a Timer.m class.
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
What I am looking to do is create a timer object in my View Controller that will update a label from the ViewController.m. Currently - the class works because I can NSLog from the Timer class and it counts down correctly. I thought about having the advanceTimer method return - but I can't seem to wrap my head around how to update the label in the ViewController with the returned data.
The only way I cold get the return to work was to have a button that refreshed the label to the correct countdown time... I can't get it to automatically count down...
Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:
If your view controller has an IBOutlet for the label and an IBAction that updates it, why not call the view-controller's action in your advanceTimer method?
Yet easier, you could connect your timer class to the label.
You might do it like this:
// Timer.h:
#interface Timer : NSObject
#property (retain, nonatomic) IBOutlet UILabel *timeLabel;
#property (assign, nonatomic) NSInteger secondsRemaining;
#property (assign, nonatomic) NSTimer *timer;
- (IBAction)startCountdown:(id)sender;
- (IBAction)stopCountdown:(id)sender;
- (void)timerFired:(NSTimer *)timer;
#end
// Timer.m
#implementation Timer
#synthesize timeLabel = timeLabel_;
#synthesize secondsRemaining = secondsRemaining;
#synthesize timer = timer_;
- (void)setTimer:(NSTimer *)timer
{
if (timer = timer_)
return;
[timer_ invalidate];
timer_ = timer;
}
- (void)scheduleTimer
{
if (self.secondsRemaining <= 0) {
self.timer = nil;
} else {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO];
}
}
- (void)timerFired:(NSTimer *)timer
{
self.secondsRemaining -= 1;
NSString *displayString = [NSString stringWithFormat:#"%d", self.secondsRemaining];
self.timeLabel.text = displayString;
[self scheduleTimer];
}
- (IBAction)startCountdown:(id)sender
{
self.secondsRemaining = 60;
[self scheduleTimer];
}
- (IBAction)stopCountdown:(id)sender
{
self.timer = nil;
}
- (void)dealloc
{
[timeLabel_ release];
[super dealloc];
}
#end
This code has a two benefits:
You can cancel your timer.
Your view controller does not need to know anything about this — you can set this up in interface builder, entirely.
Correct me if I'm wrong, but I think you should retain that NSTimer in a class member. Otherwise the timer is destroyed when finishing the method.
#property (nonatomic,retain) NSTimer * yourTimer;
In the .m file
#synthesize yourTimer;
And then
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
I fixed also your NSNumber alloc, so there are no memory leaks ;)
Explaining it, NSTimer scheduledTimerWithTimeInterval: gives you an NSTimer with autorelease. If this Timer is not retained by some member, it gets released as soon as the method ends and, since no other pointer is retaining it, it gets freed. Maybe that's the explanation ;). Never worked with NSTimers
The countdown class needs to save a link back to the ViewController and then call a method on it.
One approach would be to use the delegate pattern. Have the Countdown class's init method as initWithDelegate:(id)delegate and a predefined callback method (like updateCountdown:(NSNumber*)currentCountdown). The ViewController sends itself as the delegate and implements the update method.
Another approach is the target/action pattern. NSTimer uses this approach. The init method would be initWithTarget:(id)target selector:(SEL)selector. The ViewController sends itself as the target and whatever selector it wants to use (as long as it takes an NSNumber as it's sole argument).
In both cases in advanceTimer the Countdown class will use performSelector:withObject: to call the ViewController's update method.
If you really want a true one second timer then set it to repeat. Otherwise you will drift slowly by the amount of time the advanceTimer method takes to fire and complete. At the end of the countdown use a reference to the timer to invalidate it.

NSTimer's action method is not being called

I have the following code to create an NSTimer which should update a label each time it fires:
.h file
#interface Game : UIViewController
{
NSTimer *updateTimer;
UILabel *testLabel;
int i;
}
-(void)GameUpdate;
#end
.m file
#implementation Game
-(void)GameUpdate
{
i++;
NSString *textToDisplay = [[NSString alloc] initWithFormat:#"Frame: %d", i];
[testLabel setText:textToDisplay];
NSLog(#"Game Updated");
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01428 target:self selector:#selector(GameUpdate) userInfo:nil repeats:YES];
}
//other methods (viewDidUnload, init method, etc.)
#end
When I run it, a label appears in the top that says "0" but does not change. It makes me believe I missed something in how the NSTimer is to be setup. What did I miss?
I used breakpoints and (as you can see) logging to see if the method is actually running, rather than some other error.
I was having a similar problem, and it had a different root cause, related to the run loop. It's worth noting that when you schedule the Timer with the code:
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01428 target:self selector:#selector(GameUpdate) userInfo:nil repeats:YES];
The timer will get scheduled with the current thread's runLoop. In your case, because you make this call within the viewDidLoad, it is the main thread, so you are are good to go.
However, if you schedule your timer with a thread other than the main thread, it will get scheduled on the runLoop for that thread, and not main. Which is fine, but on auxiliary threads, you are responsible for creating and starting the initial run loop, so if you haven't done that - your callback will never get called.
The solution is to either start the runLoop for your auxiliary thread, or to dispatch your timer start onto the main thread.
to dispatch:
dispatch_async(dispatch_get_main_queue(), ^{
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01428 target:self selector:#selector(GameUpdate) userInfo:nil repeats:YES];
});
To start a runloop:
After creating a thread using your API of choice, call CFRunLoopGetCurrent() to allocate an initial run loop for that thread. Any future calls to CFRunLoopGetCurrent will return the same run loop.
CFRunLoopGetCurrent();
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01428 target:self selector:#selector(GameUpdate) userInfo:nil repeats:YES];
Your callback must have this signature:
-(void)GameUpdate:(NSTimer *)timer
This is explicitly in the docs. And the #selector() reference when you setup the timer should be #selector(GameUpdate:) (notice the trailing :).
Try that.
Just in case anyone stumbles across this, I want to point out that this:
[[NSString alloc] initWithFormat:#"Frame: %d", i];
Needs memory management.
Safely replace with:
[NSString stringWithFormat:#"Frame: %d", i];
for the same effect but no need for memory management.
P.S. At time of writing I cannot comment on the original post, so I've added this as an answer.
EDIT: As adam waite pointed out below, this isn't really relevant anymore with the widespread usage of ARC.
I have had a little bit different issue with NSTimer - scheduled method call was ignored during UITableView scrolling.
Timer had been started from main thread. Adding timer explicitly to main run loop resolved the problem.
[[NSRunLoop mainRunLoop] addTimer:playbackTimer forMode:NSRunLoopCommonModes];
Solution found here https://stackoverflow.com/a/2716605/1994889
UPD: CADisplayLink fits much better for updating UI.
According official documentation, CADisplayLink is a:
Class representing a timer bound to the display vsync.
And can be easily implemented like:
playbackTimer = [CADisplayLink displayLinkWithTarget:self selector:#selector(updateUI)];
[playbackTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
and removed like
if (playbackTimer) {
[playbackTimer removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
playbackTimer = nil;
}

UIButton Touch and Hold

I haven't found a very easy way to do this. The ways I've seen require all these timers and stuff. Is there any easy way I can hold a UIButton and cause it to repeat the action over and over until it gets released?
You can do the following: Make an NSTimer that will start up when the app starts or in viewDidLoad and also make a boolean.
For example:
//Declare the timer, boolean and the needed IBActions in interface.
#interface className {
NSTimer * timer;
bool g;
}
-(IBAction)theTouchDown(id)sender;
-(IBAction)theTouchUpInside(id)sender;
-(IBAction)theTouchUpOutside(id)sender;
//Give the timer properties.
#property (nonatomic, retain) NSTimer * timer;
Now in your implementation file (.m):
//Synthesize the timer
#synthesize timer;
//When your view loads initialize the timer and boolean.
-(void)viewDidLoad {
g = false;
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
}
Now make an IBAction for "Touch Down" set the boolean to lets say true. Then make another IBAction button for "Touch Up Inside" and "Touch Up Outside" assign the boolean to false.
For example:
-(IBAction)theTouchDown {
g = true;
}
-(IBAction)theTouchUpInside {
g = false;
}
-(IBAction)theTouchUpOutside {
g = false;
}
Then in that NSTimer method, put the following:(assume g is the boolean you have declared)
-(void) targetmethod:(id)sender {
if (g == true) {
//This is for "Touch and Hold"
}
else {
//This is for the person is off the button.
}
}
I hope this simplifies everything... I know it still uses a timer but there is not another way.
Unfortunately, it still looks like you have to code this functionality for yourself. simplest way (You still need a timer though):
A function that performs the action you want to repeat:
-(void) actionToRepeat:(NSTimer *)timer
{
NSLog(#"Action triggered");
}
in your .h file declare and set a property for a timer:
#interface ClassFoo
{
NSTimer* holdTimer;
}
Then in the .m make two IBActions:
-(IBAction) startAction: (id)sender
{
holdTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(actionToRepeat:) userInfo:nil repeats:YES];
[holdTimer retain];
}
-(IBAction) stopAction: (id)sender
{
[holdTimer invalidate];
[holdTimer release];
holdTimer = nil;
}
Then Just link to the Touch Down event in IB from the button to startAction and the Touch Up Inside to the 'Stop Action'. It isn't a one liner but it allows you to customise the rate the action repeats as well as allowing you to trigger it from another outlet/action.
You might consider subclassing UIButton and adding this functionality if you are going to be using this functionality often - then it is only (slightly) painful to implement the first time.
An other way to use this NBTouchAndHoldButton. This is exactly what you want, and very easy to implement it:
TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
[pageDownButton addTarget:self action:#selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2];
Good luck!
I cannot reply to the first one, but this line:
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
for at least iOS 4.1 and newer needs to be:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
I know this is an old question, but as an easy way, like to consider using "[NSObject performSelector:withObject:afterDelay:]" to repeatedly invoke methods in any particular time interval.
In this case:
NSTimeInterval someTimeInterval = 1;
- (IBAction)action:(id)sender {
UIButton * const button = sender;
if (button.state != UIControlStateHighlighted) {
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender];
[self performSelector:_cmd withObject:sender afterDelay:someTimeInterval];
}