Reputation: 514
My problem is as below:
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop1) userInfo:nil repeats:YES];
timer2 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop2) userInfo:nil repeats:YES];
timer3 = [NSTimer scheduledTimerWithTimeInterval:1.0/20 target:self
selector:@selector(Loop3) userInfo:nil repeats:YES];
timer4 = [NSTimer scheduledTimerWithTimeInterval:1.0/10 target:self
selector:@selector(Loop4) userInfo:nil repeats:YES];
timer5 = [NSTimer scheduledTimerWithTimeInterval:1.0/5 target:self
selector:@selector(Loop5) userInfo:nil repeats:YES];
I have used these 5 timers to move frames on iPhone. But timer3
and timer4
behave differently on iPod and simulator. timer3
and timer4
are slower on iPod than what I want to implement, and works fine on simulator.
Please suggest where the problem is?
Upvotes: 2
Views: 1332
Reputation: 70733
NSTimers cannot schedule 2 or more methods to start at the same time (especially on an iOS device with only 1 processor core). If the first timer task is slow, the second one will be late.
On the Simulator, the first task may well run 10X or more faster (due to raw CPU and memory performance) making the second task so much less late that you don't notice that it is late.
Either make each task faster, or skew the timers so that the tasks don't overlap. Or combine what's done inside each timer task, if that task occurs at some least common multiple time slot.
Upvotes: 1
Reputation: 21903
According to the docs: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds
Your two troublesome timers are 1/10 and 1/20 of a second, which happen to be exactly 100 and 50 milliseconds. You're flirting with the finest slice of time an NSTimer can handle, and you should expect some unreliability.
Upvotes: 0
Reputation: 46037
You can not rely on timer if you require high precision. This will depend on device load. The simulator is running on Mac, so you will get higher precision. But the device is less less powerful than the Mac. So there will be obvious delay.
Why do you need timers in this way? Are you trying to animate something? In that case there are better options available. Specially look into UIView animation instead of timer.
Upvotes: 0
Reputation: 9113
Your problem might just be a performance issue. Check using the Activity Monitor Tool and the application running on the device if the device doesn't just run out of resources.
My guess is that you're asking the device to repeat three operations every 10th of a second and that might be out of its capacity depending on the operation.
Maybe you could drop the lines of code of Loop3 and Loop4 ?
Upvotes: 0