Re: NSTimer
Re: NSTimer
- Subject: Re: NSTimer
- From: Matthew Holiday <email@hidden>
- Date: Wed, 31 Aug 2005 16:28:14 -0500
Curiouser and curiouser. I changed my call from +
scheduledTimerWithTimeInterval: to +timerWithTimeInterval: (since I
was already adding it to the run loop) and the timer still "works."
By that I mean, that if there's something else going on in the run
loop, it fires and calls my selector, but not all that often (I have
a timeout of 0.05 seconds). All I can think of in my case is that
there's enough work going on in the protocol stack my thread is
running that the run loop never gets to evaluate the timer for
(relatively) long periods of time.
The timer fires about every other time I get a record over the
connection. If I set the timer to 0.5 seconds, it fires on time, but
one of the record retrieval operations becomes a good bit longer; I
still get two retrievals before the timer fires, but things run more
slowly. I'd think that I'd get more records in between timer firings.
Very odd. I should point out that I'm "scheduling" each retrieval by
sending the run loop a -performSelector: call once for each record to
ensure the run loop has a chance to run in between.
I'm not using an invocation when I create my timer, but just giving
it a selector and a reference to self. From my quick reading on
NSInvocation, it would appear that an invocation needs a target, and
I don't see in your code where one is set. You might change your code
from using an invocation to using a target/selector instead and see
what happens (different class factory method on NSTimer).
On Aug 31, 2005, at 2:30 PM, Christopher Hickman wrote:
Not according to the docs:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
Returns a new NSTimer object and adds it to the current NSRunLoop
in the
default mode. After seconds have elapsed, the timer fires, sending the
message in invocation to its target. If seconds is less than or
equal to
0.0, this method chooses a nonnegative interval. If repeats is YES,
the
timer will repeatedly reschedule itself until invalidated. If
repeats is NO,
the timer will be invalidated after it fires.
-----Original Message-----
From: cocoa-dev-bounces+tophu=email@hidden
[mailto:cocoa-dev-bounces+tophu=email@hidden] On Behalf Of
email@hidden
Sent: Wednesday, August 31, 2005 3:16 PM
To: email@hidden
Subject: Re: NSTimer
I think you need to add something like
[[NSRunLoop currentRunLoop] addTimer: timer forMode:
NSDefaultRunLoopMode];
for your timers in order to have the run loop run them; otherwise,
I think
you've created objects that actually aren't going to do anything
but sit
there.
On Wednesday, August 31, 2005 Christopher Hickman <email@hidden>
wrote:
Message: 2
Date: Wed, 31 Aug 2005 14:38:58 -0400
From: Christopher Hickman <email@hidden>
Subject: NSTimer
Hi folks,
I have an NSTimer that I'm trying to use to periodicly call a
method.
The timer gets created, but it doesn't call my method when it fires.
Here's my code, do you see anything wrong?
-(void)incrementProgressIndicator;
{
NSLog(@"incrementProgressIndicator"); //Never logs
//progressIndicator is an NSProgressIndicator IBOutlet of this
object
//incrementAmount is a double ivar with a written accessor
if (progressIndicator != nil) {[progressIndicator incrementBy:
[self
incrementAmount]];}
}
-(void)incrementProgressIndicatorEveryTimeInterval:
(NSTimeInterval)seconds iterations:(int)iterations;
{
NSLog(@"incrementProgressIndicatorEveryTimeInterval: %f
iterations:
%i", seconds, iterations);
NSTimer *incrementerTimer;
NSInvocation *incrementerInvocation;
incrementerInvocation = [NSInvocation
invocationWithMethodSignature:
[self methodSignatureForSelector: @selector
(incrementProgressIndicator)]];
incrementerTimer = [NSTimer
scheduledTimerWithTimeInterval:seconds
invocation:incrementerInvocation repeats:YES];
//incrementerTimer needs to be invalidated after it fires
(iterations) times, so we set up a
//second timer to wait (seconds * iterations) seconds and
invalidate
the original timer
[NSTimer scheduledTimerWithTimeInterval:(seconds*iterations)
invocation:[NSInvocation invocationWithMethodSignature:
[incrementerTimer
methodSignatureForSelector:@selector(invalidate)]] repeats:NO];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >RE: NSTimer (From: "Christopher Hickman" <email@hidden>) |