Converting from Carbon Event Manager to NSTimer
Converting from Carbon Event Manager to NSTimer
- Subject: Converting from Carbon Event Manager to NSTimer
- From: Dan Birns <email@hidden>
- Date: Tue, 23 Sep 2008 16:20:29 -0700
I'm trying to convert from Carbon to Cocoa for a number of reasons
which I won't go into here.
My application needs to set a timer that causes a function to be
called at a time in the future. This is non-repeating, and sometimes
has be immediate. I need it to be as efficient as possible, because
it's called frequently.
In Carbon I'm using:
EventLoopTimerUPP upp;
upp = NewEventLoopTimerUPP(macTimerCallback);
InstallEventLoopTimer(GetMainEventLoop(),0, kEventDurationForever,
upp,0,&macTimer);
Then, when I need to set the timer:
nextTimerTime = CFAbsoluteTimeGetCurrent() + nextTimer;
SetEventLoopTimerNextFireTime(macTimer, nextTimer);
In Cocoa, the system I'm using requires me to allocate a new NSTimer
every time I need to set a timer:
self.timer = [NSTimer scheduledTimerWithTimeInterval: t // seconds
target: self
selector: @selector (mainLoopTimer:)
userInfo: nil
repeats: NO];
This is working fine, but our performance is poor. It's not so poor
that it's obviously broken, but I'm looking for ways to improve it.
I've been try to alloc one NSTimer that I reuse, and I've had no
success doing so.
I've tried
timer = [NSTimer alloc];
[timer initWithFireDate:[NSDate date] interval:t target:self
selector:@selector(mainLoopTimer:) userInfo:halTimer repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer
forMode:NSDefaultRunLoopMode];
This has failed. Investigating, I called [timer isValid] and it
returns false. I've tried altering the args in various ways without
success. I've also had various problems retain'ing this timer. When
I add [timer retain];, it crashes having exhausted the stack, where
it's calling retain over and over.
Help.
--Daniel
_______________________________________________
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