• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
to implement a 1 sec repeating timer in a separate thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

to implement a 1 sec repeating timer in a separate thread


  • Subject: to implement a 1 sec repeating timer in a separate thread
  • From: Nick Rogers <email@hidden>
  • Date: Thu, 22 Oct 2009 13:48:56 +0530

Hi,
I have been trying to implement a 1 second repeating timer, but its leading to retaining the target object of a thread from which I'm launching this timer thread.
Here's my implementation.


I'm running this method from my thread.
- (void)setUpTimer
{
timerThread = [[NSThread alloc] initWithTarget:self selector:@selector (startTimer) object:nil];
[timerThread start];
}


- (void)startTimer
{
timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(myTimerFireMethod:) userInfo:nil repeats:YES];
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
[theRL addTimer:timer forMode:NSDefaultRunLoopMode];
while (keepTimerRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
}


- (void)myTimerFireMethod:(NSTimer*)theTimer
{
	[self setUpdateStatus:YES];
}

From my thread I'm doing something when updateStatus is YES and then I'm setting it to NO, which is again set to YES by this timer, so that I get to do something at 1 second interval.
But its leading to referencing the target object of my thread, by 3-4 non-objects (bytes), which shows different descriptions every time as per GC Monitor Instrument (kind of undefined behavior).


Whats wrong with the above scheme, or how can I make it safe?
Googled a lot but couldn't find a simple timer example that would suit my need. Can you point me to some example code etc.?


Wishes,
Nick

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: to implement a 1 sec repeating timer in a separate thread
      • From: Dave Keck <email@hidden>
  • Prev by Date: Re: Sending a Selector to another Class.
  • Next by Date: Re: to implement a 1 sec repeating timer in a separate thread
  • Previous by thread: Re: NSNotificationCentre very slow - normal?
  • Next by thread: Re: to implement a 1 sec repeating timer in a separate thread
  • Index(es):
    • Date
    • Thread