Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-05-2009, 05:46 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 223
Default NSTimer not firing

Hey all,

I'm running into a strange problem in my app. I'm using an NSTimer as a countdown timer before beginning a new 'round'. The idea is that the timer starts with an initial value and then decrements each time the timer fires. This then shows up on an alert view I have showing. All of the logic for this works and is in place.

The problem is that my timer isn't firing!

You'll have to forgive my pseudocode, but I think it will make more sense than my present code.

In my view controller
Code:
-(void)loadingFinished
{
show an alert view with %d seconds remaining, where %d is gotten from a property of the timerManager class
//Start the countdown
tell the timerManager to start the countdown timer
}

Other code including notification selectors for notifications sent by the timerManager class
and in my timerManager class
Code:
-(void)startCountdownTimer
{
create a scheduled timer calling a selector within this class
}

..Selector called by the timer which dispatches a notification
The thing is, I've stuck NSLog statements into the selector in timerManager, and the selector is never called. The timer exists and is valid and everything, but it never fires (not even with an unrecognized selector)

I've tried removing the alertView to see if that's causing the problem (modal blocking or something), but no luck there either. Same problem.

Why on earth wouldn't the timer be firing?

-CB
Chilibird is offline   Reply With Quote
Old 10-05-2009, 05:51 AM   #2 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
Default

Well, we wouldn't really know what was going on or what was wrong, since you commented out the code.

If you supply us with your code, we could help you see what's wrong.
__________________
Bacteria Bash
Cheese Collect
Jokestar
Follow me on Twitter for news about my apps:
(Website|Twitter)
kohjingyu is offline   Reply With Quote
Old 10-05-2009, 05:56 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 223
Default

In the view controller. alertView is a UIAlertView that is created to display loading information. Getting rid of this view has no effect on the behavior of the timer.
Code:
-(void)songLoadingFinished
{
	alertView.title = @"Loading finished";
	alertView.message = [NSString stringWithFormat:@"Beginning in %d seconds", timeManager.timeBetween];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(songCountdownTimerFired) name:kSongCountdownTimerFired object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(songCountdownTimerFinished) name:kSongCountdownComplete object:nil];
	
	[timeManager startSongCountdownTimer];
}
in timeManager
Code:
-(void)startSongCountdownTimer
{
	self.countdownTime = kDefaultTimeBetween;
	
	songCountdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(songCountdownTimerFired:) userInfo:nil repeats:YES];
}

-(void)songCountdownTimerFired:(NSTimer *)theTimer
{
	NSLog(@"Time remaining is %d", self.countdownTime);
	
	if (self.countdownTime > 0)
	{
		self.countdownTime = self.countdownTime - 1;
		
		[[NSNotificationCenter defaultCenter] postNotificationName:kSongCountdownTimerFired object:nil];
	}
	else 
	{
		[[NSNotificationCenter defaultCenter] postNotificationName:kSongCountdownComplete object:nil];
	}

}
The NSLog statement in songCountdownTimerFired: is never called.
Chilibird is offline   Reply With Quote
Old 10-05-2009, 06:01 AM   #4 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
Default

Hmm, perhaps you have some other code but I don't think

-(void)songCountdownTimerFiredNSTimer *)theTimer

Is needed.
__________________
Bacteria Bash
Cheese Collect
Jokestar
Follow me on Twitter for news about my apps:
(Website|Twitter)
kohjingyu is offline   Reply With Quote
Old 10-05-2009, 06:14 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 223
Default

It isn't and I really have it in there for correctness, but changing that (and the corrisponding selector signature) has had no effect.

Like I said... I can't imagine the run loop is blocking or anything since the alert view has no effect. Manually creating a timer and adding it to the run loop ends up with the same results too.
Chilibird is offline   Reply With Quote
Old 10-05-2009, 07:54 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 223
Default

*bump*

I tried calling a timer from within the view controller to see if any timer is firing. No luck there either. This exists whether the UIAlertView is there or not

Has anyone else had this issue/has any idea what is going on?


Edit: UPDATE: The issue appears to be associated with a separate thread I'm calling in another class, as commenting out the actual 'loading' process fixes this. I guess I'll just have to keep digging :P

Edit: SOLVED: Turns out that by sending a notification from the branched thread, the timer was called on the branched thread, and when that thread terminated, the timer didn't exist. Whoopsies :P
Using a 'performSelectorOnMainThread:' call fixed it.

Last edited by Chilibird; 10-05-2009 at 08:05 PM.
Chilibird is offline   Reply With Quote
Old 03-11-2010, 01:27 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 32
Default

Quote:
Originally Posted by Chilibird View Post
*bump*

Edit: SOLVED: Turns out that by sending a notification from the branched thread, the timer was called on the branched thread, and when that thread terminated, the timer didn't exist. Whoopsies :P
Using a 'performSelectorOnMainThread:' call fixed it.
Hi, I think I'm having the same problem and I was hoping you could provide a little code or advice. I have tried this to make sure it's on the main thread, but neither of my NSLogs are happening, so it should already be on the main thread and the timer is never being fired.
Code:
	
-(void)viewWillAppear:(BOOL)animated {
...other code...
	_countdown = 15;
	if ([NSThread currentThread] != [NSThread mainThread]) {
		NSLog(@"not the main thread");
		[self performSelectorOnMainThread:@selector(createTimer) withObject:nil waitUntilDone:YES];
	} else {
		[self createTimer];
	}
}
-(void) createTimer {
	_countdownTimer = [[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES] retain];
}
-(void)updateCountdown {	
	NSLog(@"yo");
	if (_countdown > 0) {
		_countdown--;
	} else {
		[_countdownTimer invalidate];
		[_countdownTimer release];
		_countdownTimer = nil;
		...do other stuff...
	}
}
Any thoughts on why this timer is not firing?
jazztpt is offline   Reply With Quote
Old 03-11-2010, 10:30 PM   #8 (permalink)
Will Work for Food!
 
itzdark's Avatar
 
Join Date: Apr 2009
Posts: 579
Send a message via AIM to itzdark Send a message via MSN to itzdark
Default

Use scheduledTimerWithTimeInterval instead. The one you used will only fire when added to a run loop.


Quote:
Originally Posted by jazztpt View Post
Hi, I think I'm having the same problem and I was hoping you could provide a little code or advice. I have tried this to make sure it's on the main thread, but neither of my NSLogs are happening, so it should already be on the main thread and the timer is never being fired.
Code:
	
-(void)viewWillAppear:(BOOL)animated {
...other code...
	_countdown = 15;
	if ([NSThread currentThread] != [NSThread mainThread]) {
		NSLog(@"not the main thread");
		[self performSelectorOnMainThread:@selector(createTimer) withObject:nil waitUntilDone:YES];
	} else {
		[self createTimer];
	}
}
-(void) createTimer {
	_countdownTimer = [[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES] retain];
}
-(void)updateCountdown {	
	NSLog(@"yo");
	if (_countdown > 0) {
		_countdown--;
	} else {
		[_countdownTimer invalidate];
		[_countdownTimer release];
		_countdownTimer = nil;
		...do other stuff...
	}
}
Any thoughts on why this timer is not firing?
__________________

Check out my apps

Developers, check out study buddy. I use it everytime I code. It's great for those late night coding sessions.
Unofficial Ad Hoc Distribution Guide || Join my cooperative ad hoc testing group
iSoothe Promotional Video
Contact Me
itzdark is offline   Reply With Quote
Old 03-13-2010, 09:31 AM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 32
Default

Quote:
Originally Posted by itzdark View Post
Use scheduledTimerWithTimeInterval instead. The one you used will only fire when added to a run loop.
You are so right! Thanks!
jazztpt is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 239
21 members and 218 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, FAED, fredidf, iDifferent, iph_s, JamesCahall, JasonR, mer10, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, timle8n1, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:13 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0