scheduledTimerWithTimeInterval not updating timer method while scrolling? - ios

I have strange issue with timer. timer is updating well within application. i am showing the code.
// Start Timer
#pragma mark- Timer
- (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
}
// Update Timer
- (void)updateTimer:(NSTimer *)theTimer
{
NSLog(#"called");
}
Problem: I have a textview when i scroll text within the
textview the updateTimer method stops calling and when I stop
scrolling then it starts to update timer.
Now what to do to continue calling the update timer method?

For do fix this issue you need to add your NSTimer to the mainRunLoop. Such like,
- (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

You can do this way also,
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:#selector(updateTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer
forMode:NSRunLoopCommonModes];

Related

Display a message using NSTimer

NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
How can I use for display the message "your time is over" after 5 min?
In below code, Instead 5.0 you can put your time in second
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(targetMethod) userInfo:nil repeats:NO];
-(void)targetMethod {
//enter code here to execute...
}

NSTimer not firing, has correct method signature

I'm using an NSTimer to call a method every second, but it doesn't work. I can call it manually using [refreshTimer fire]; but otherwise it doesn't work. Any ideas why?
Here is where I initialize the timer:
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(sendRefreshPacket:) userInfo:nil repeats:YES];
And then the method:
- (void)sendRefreshPacket: (NSTimer*) timer
{
NSLog(#"Test");
}
Try this:
dispatch_async(dispatch_get_main_queue(), ^{
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(sendRefreshPacket:) userInfo:nil repeats:YES];
});

Run NSTimer in background thread to call method on completion in main thread

I have a static method that creates a NSTimer and runs it in the background thread, like so:
+ (void) callInBackgroundThread {
NSTimer *timer = [NSTimer timerWithTimeInterval:0.2
target:self
selector:#selector(callToMainThread)
userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
and then i call the main thread upon completion like so:
+ (void) callToMainThread{
NSTimer *timer = [NSTimer timerWithTimeInterval:0
target:self
selector:#selector(foo1)
userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
While this works i feel that this is quite sketchy and I wonder if there is a better way of doing this.
I would appreciate suggestions, please note that the methods where are static.
Any help would be appreciated.
Regards
performSelectorOnMainThread:withObject:waitUntilDone: also works for classes!
+ (void) callToMainThread {
[self performSelectorOnMainThread:#selector(foo1) withObject:nil waitUntilDone:NO];
}

NSTimer strange behaviour

I have an NSTimer and I get a different results when I call a method or I try to fire that method using the timer.
NSTimer *timer = [NSTimer timerWithTimeInterval:animationStepDuration
target:self
selector:#selector(handleAnimation:)
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:newScale], #"scale",
[NSValue valueWithCGPoint:offsetToCenter], #"offset",nil]
repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
The result of handleAnimation: is different than this one.
Can someone tell me why?

NSTimer - sequence of activities - how to invalidate timer?

I would like to make a sequence of activities (A and B) using 2 timers.
I get some problems with timer invalidate...
Is this the correct way to proceed?
Thank you!!!
timerAStart = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(startActivityA) userInfo:nil repeats:NO];
timerAStop = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(StopA) userInfo:nil repeats:NO];
timerBStart = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(startActivityB) userInfo:nil repeats:NO];
timerBStop = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:#selector(StopB) userInfo:nil repeats:NO];
- (void) StopA {
[timerAStart invalidate];
timerAStart=nil;
}
- (void) StopB {
[timerBStart invalidate];
timerBStart=nil;
}
You don't need to invalidate since your timer is non-repeating. If you look at the documentation you'll see that non-repeating timers self-invalidate.

Resources