Unable to invalidate (Stop) NSTimer [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
I am using a NSTimer to update the value of a slider while a audio is playing.
if (sliderUpdateTimer) {
[sliderUpdateTimer invalidate];
sliderUpdateTimer=nil;
}
sliderUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateSliderForAudio) userInfo:nil repeats:YES];
once the audio is finished playing i am invalidating the Timer in audioPlayer delegate method.
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(#"audioPlayerDidFinishPlaying");
[player release];
audioPlayer=nil;
[sliderUpdateTimer invalidate];
[sliderUpdateTimer release];
sliderUpdateTimer=nil;
}
The delegate method is getting called but the timer is not stopping.... I have only once thread on which i am runing the timer. But still the timer does not stop.... Any help in this regard is welcome...

First of all, you would be over releasing it and should receive EXC_BADACCESS (you're not retaining the timer when you set it, so you shouldn't release it either). Should only call:
[sliderUpdateTimer invalidate];
sliderUpdateTimer=nil;
Since you don't receive a crash, it seems sliderUpdateTimer is nil when calling audioPlayerDidFinishPlaying:. User NSLog or put a breakpoint to check if this is true. If this is the case, you're probably setting it to nil at some point, search for slideUpdateTimer and check where this might occur.

Hey try this thing...
- (void)stopTimer{
[sliderUpdateTimer invalidate];
//don't release it. as it is autoreleased.
//[sliderUpdateTimer release];
sliderUpdateTimer=nil;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(#"audioPlayerDidFinishPlaying");
[player release];
audioPlayer=nil;
[self performSelectorOnMainThread:#selector(stopTimer) withObject:nil waitUntilDone:NO];
}
I know you have only single thread but there may be two different run loops

Try after removing the below lines from 1st section of your code
if (sliderUpdateTimer){
[sliderUpdateTimer invalidate];
sliderUpdateTimer=nil;
}
Don't need to call release message on that sliderUpdateTimer object
Because you have created that object with pending autorelease message
Here no need to use these lines.
I hope this will work.

Related

NSTimer some times getting fired before the fire date?

I'm setting up a timer to fire at an interval of 13 mins. But the timer seems to be getting fired at irregular intervals. This is an intermittent issue. Some users reported the issue and I'm not able to reproduce the same.
- (void)resetIdleTimer
{
if (_idleTimer)
{
[_idleTimer invalidate];
[_idleTimer release];
_idleTimer = nil;
}
_idleTimer = [[NSTimer scheduledTimerWithTimeInterval:13*60.0
target:self
selector:#selector(idleTimerExceeded)
userInfo:nil
repeats:NO] retain];
}
- (void)idleTimerExceeded
{
// do some processing
[_idleTimer release];
_idleTimer = nil;
}
Timer will be reset (resetIdleTimer) depending on some conditions, but that's anyhow resets the timer with 13 mins.
When I looked into code, I can see only the issue that is not passing timer parameter to the selector. I'm not sure whether that's the reason for this issue or not ? Did anyone come across this kind of weird ness ? (Any how I will be updating the code to to have timer as an argument).
One user reported that its just happened after 4 mins itself.
Let confirm one thing,not passing timer parameter to the selector will not cause any issues.
Here is the updated code which works fine
#property(nonatomic,strong)NSTimer *myTimer;
or
#property(nonatomic,retain)NSTimer *myTimer;
Methods
- (void)resetIdleTimer
{
if ([self.myTimer isValid])
[self.myTimer invalidate];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:13*60.0
target:self
selector:#selector(idleTimerExceeded)
userInfo:nil
repeats:NO];
}
- (void)idleTimerExceeded
{
NSLog(#"tiggered");
}
i don't find any error in your code, the only idea about is that a user enter your class creating an instance, let's call it instance "A", then start the timer, then exit your class before 13 minutes, then enter again with a new instance "B", a new timer starts...
at this point user expects your timer methods fires after 13 minutes... but timer of "A" is still pending (because there may be an error in the way you close your class) and executes...
You may try to add some logs and try to do as i said, and check all logs (in particular if you enter the dealloc, and that the log in idleTimerExceeded print instances equals to the last instances printed in resetIdleTimer):
-(void)resetIdleTimer {
if (_idleTimer)
{
[_idleTimer invalidate];
[_idleTimer release];
_idleTimer = nil;
}
NSLog(#"...resetIdleTimer: instance: %p", self);
_idleTimer = [[NSTimer scheduledTimerWithTimeInterval:13*60.0
target:self
selector:#selector(idleTimerExceeded)
userInfo:nil
repeats:NO] retain];
NSLog(#"...resetIdleTimer: _idleTimer: %p", _idleTimer);
}
- (void)idleTimerExceeded
{
// do some processing
NSLog(#"...idleTimerExceeded: instance: %p", self);
NSLog(#"...idleTimerExceeded: _idleTimer: %p", _idleTimer);
[_idleTimer release];
_idleTimer = nil;
}
-(void) dealloc
{
NSLog(#"...dealloc: instance: %p", self);
[super dealloc];
}
just an idea... but you may have forgotten that timers retain the instance passed in target:parameter (you pass "self", so your class instance is retained), so a common error is to forget to close the timer when closing the class instance.
a correct way should be:
-a root class (AAA) instantiate your class (BBB) with timer
-BBB start its timer
-AAA wanna dealloc/resetIdleTimer class BBB: before to release it must stop/invalidate BBB.timer (if not timer retain BBB, and BBB won't be released until timer fires/executes its method)
so, if it's your case... add a method that could be called from class AAA that stop/invalidate the timer, something like:
-(void)stopTimer{
if (_idleTimer)
{
[_idleTimer invalidate];
[_idleTimer release];
_idleTimer = nil;
}
}
If you want your timer to fire in exact 13 minutes, maybe this will help:
https://stackoverflow.com/a/3519913/1226304

Image is already being captured or camera not yet ready

I am calling this takePicture function 5 times because i neeed to take 5 picture on on click(Burst Mode)
for(count=0;count<5;count++)
{
[picker takePicture];
[NSThread sleepForTimeInterval:0.5];
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
}
I am getting this error UIImagePickerController: ignoring request to take picture; image is already being captured or camera not yet ready.
Not sure but i think camera not yet ready ... Because you are trying to capture images continuously.... I think you will have to delay for few seconds before call take picture method again..... Dont do it in for loop i would like to suggest you please use NSTimer instead of looping.
something like this -
Declare
-(void)startTimer;
and
int count;
in your .h class then see below code -
-(void)yourTakePictureButtonClick:(id)sender
{
[self startTimer];
}
-(void)startTimer
{
count = 0;
yourTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(myFunctionForClickImage) userInfo:nil repeats:YES];
}
-(void)myFunctionForClickImage
{
[picker takePicture];
count ++;
if (count < 5)
{
[yourTimer invalidate];
}
}
Hi H2SO4 (Nice Name han)
The most probable reason seems to be the absence of the required key in your info.plist file. You will need to configure UIRequiredDeviceCapabilities. Also, you must implement the delegate object. For details, have a look at
http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/TakingPicturesAndMovies.html#//apple_ref/doc/uid/TP40010406.
HTH,
Another thing you should release your resources in every call inside loop.
You can delay with following.
[picker performSelector:#selector(takePicture) withObject:nil afterDelay:2.0];
For more you can visit...
http://highoncoding.com/Articles/856_Building_Instagram_for_the_iPhone_Part_2.aspx

How to pause and resume NStimer [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I programmatically pause an NSTimer?
I have a question. How can I pause a countdown using a timer? I am developing a game. In the game, I need to go to next view when the timer pauses, and after coming back I want to resume it.
I try this code in the view:
[mytimer pause];
// to resume
[mytimer resume];
I try that code, but I get a warning saying: "NSTimer may not respond to 'pause'"
I build with that warning and when I press the pause button, the app crashes.
NSTimer indeed doesn't have resume and pause methods so you can end up with a crash in runtime after such a warning. Generally you can create 2 kinds of timers (see NSTimer class reference) one that implements only once and the second, that repeats. Example:
This way you create a timer, that will enter callback myMethod each second.
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(myMethod:)
userInfo:nil
repeats:YES];
You probably will choose this one for your purpose where in your class you should maintain some
BOOL pausevariable and in the callback myMethod do the following:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
where you update pause accordingly somewhere in your code.
To stop the timer (and release it) call
[myTimer invalidate];
good luck
What you want, is what OpenGLES application brings up to you. You should create 2 methods like this:
- (void)startAnimation
{
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:#selector(selector) userInfo:nil repeats:YES];
}
- (void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
}
It's something like this.
Refer to the NSTimer Class Reference, there is no pause method.

NSTimer not stopping

I have a Class with a NSTimer *myTimer; variable. At some point I do:
myTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:#selector(doStuff) userInfo:nil repeats: YES];
further, I have a method:
- (void)doStuff
{
if(myTimer)
{
//do stuff
}
}
and I stop my timer when the class is released through:
- (void)dealloc
{
if (myTimer) { //if myTimer==nil it already has been stopped in the same way
[myTimer invalidate];
myTimer = nil;
}
}
Now, the problem is that when I release the class the timer goes on and on and on firing the event anyway. Am I doing something wrong? It seems the dealloc method is never called, otherwise myTimer would be nil and even if the selector is fired it would not go into the if(myTimer)
This will never work, because timers retain their target, which means your dealloc method will never get invoked until after you've invalidated the timer.
For more info, see the NSTimer documentation and this blog post on "Dangerous Cocoa Calls"
Have you tried the handy debugger tools at your disposal? What happens if you set a breakpoint in your dealloc method? Also, you should post more context around your creation. Is it possible you're creating the timer more than once, thereby replacing the original (but not invalidating it) and leaving it out there to fire at will?

How to know a movieplayer release finished?

I aims to release a movieplayer (theMovie) and then start another action (so-called playButtonClicked) after it is completely released. I used performSelector to delay the "playButtonClicked" for 1 second and it works well. The code is:
[theMovie release];
[self performSelector:#selector(playButtonClicked) withObject:nil afterDelay:1];
However, I don't want to always delay 1 second. I want to start the "playButtonClicked" as soon as "theMovie" is completely released. I tried the following code, but it didn't work because [timer userInfo] never is nil.
Does anybody know how to check a movieplayer release finished.
[theMovie release];
//[self performSelector:#selector(playButtonClicked) withObject:nil afterDelay:1];
NSTimer *atimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:#selector(waitForReleaseFinish:)
userInfo: (MPMoviePlayerController*) theMovie repeats:YES];
The code of waitForRleaseFinish: (NSTimer *)timer is:
if ([timer userInfo]==nil) //here I actually want to test if (theMovie==nil),but I don't know how to do and I'm not sure if it is a correct way to determine the release finished.
{
[timer invalidate];
[self playButtonClicked];
}
Look forward to helps. Thank you.
There is no need.
If you just release the player and then call playButtonClicked, like this:
[theMovie release];
[self playButtonClicked];
It won't execute the second line until the first is completed, or until theMovie is released. This is all on the same thread so it will execute in order. You don't need a timer for this. Although in situations where what you're waiting for to finish executes on a new thread, you would use a callback, rather than guessing how long it takes (which is much less than 1 second!).
Also, just so you don't misunderstand, "completely releasing" is just subtracting the retainCount by one. It will automatically deallocate when it reaches zero.
Just as a side note, why is it important that theMovie is released (deallocated?) before playButtonClicked is executed?
Also, your waitForReleaseFinish: code would work, but it's unnecessary because theMovie would be released before the timer is created.