Re: Asynchronous timers (without a run loop)
Re: Asynchronous timers (without a run loop)
- Subject: Re: Asynchronous timers (without a run loop)
- From: "Påhl Melin" <email@hidden>
- Date: Sat, 6 Dec 2008 00:22:29 +0100
2008/12/5 Sherm Pendley <email@hidden>:
> On Dec 5, 2008, at 3:16 PM, Påhl Melin wrote:
>
>> 2008/12/5 Sherm Pendley <email@hidden>:
>>>
>>> You'd have far, far less trouble programming for the Mac if you'd simply
>>> learn how Cocoa works, instead of trying to reinvent .NET in Objective-C.
>>> When in Rome, do as the Romans do.
>>
>> That's why I asked in the first place. I didn't know how Cocoa works
>> in regards to timers.
>
> I understand that - and it's a good point. But consider how you asked the
> question. If you don't know how Cocoa works, how is it that you've already
> decided that you shouldn't use a run loop? That's putting the cart in front
> of the horse, IMHO. The fact that you don't use a run loop to build timers
> with some other framework doesn't imply that you shouldn't do so when you're
> using Cocoa.
I didn't really decide I shouldn't use a run loop for the timers. I am
porting a library from another platform and just wanted a quick answer
if I could get a similar timer API behavior in either Cocoa, Core
Foundation or BSD Unix (with a preference on low-level for
efficiency), as I was already using. It would save me time and make
the resulting code simpler and probably more robust (since I'm not an
Cocoa/CF expert).
> Unless I've missed something, you have a classic A/B situation. You want to
> do task A - fire a timer event every half second, starting five seconds from
> now. And based on your experience with .Net and BSD, you've decided that
> method B - threading without run loops - is how you want to do that, so
> you're asking for help with method B. The problem is that the method B isn't
> the best match for Cocoa's way of doing things, and actually makes task A
> far more difficult than it normally would be. You need to take a step
> further back, and find out how Cocoa does task A.
>
> So, how to do task A the "Cocoa way?" Here's a simple example:
>
> - (void)startTimer {
> NSTimer * t = [NSTimer scheduledTimerWithTimeInterval: 4.5
> target: self
> selector: @selector(delayTimerFired:)
> userInfo: nil
> repeats: NO
> ];
> }
>
> - (void)delayTimerFired:(NSTimer *)theTimer {
> // Create interval timer
> NSTimer * t = [NSTimer scheduledTimerWithTimeInterval: 0.5
> target: self
> selector: @selector(intervalTimerFired:)
> userInfo: nil
> repeats: YES
> ];
> }
>
> - (void)intervalTimerFired:(NSTimer *)theTimer {
> // do stuff every half second
> }
>
> If you want to invalidate the timer at some future point, you could do so
> directly by storing a reference to it in an instance variable. Or, you could
> check for some condition in -intervalTimerFired:, and send [theTimer
> invalidate] if needed.
Thanks for the explanation on Cocoa timers.
/ Påhl
_______________________________________________
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