views:

71

answers:

4

Hi frends my problem is as under:

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 behaves 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 me where is the problem.

Thanx.

A: 

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 ?

VdesmedT
i have 5 objects to move at a particular speed. I can't drop any loop.
Jack
Why can't you rely on core animation for that then ?Have you tried the [UIView beginAnimation] way of doing animations ?
VdesmedT
I can't use UIAnimation bcz I treat them as a sprite and want to set some properties and also detect some action with them.I have solved my problem by combining timers as hotpaw2 said.
Jack
A: 

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.

taskinoor
I had used NSTimer in my previous application with scheduledTimeInterval 1/30 and that works fine.but in this app 1/20 behaves like 1/5.
Jack
this depends on your app, what u r doing in the loop etc. until the run loop completes it's current cycle, next timer wont be fired.
taskinoor
A: 

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.

Dan Ray
but i had used timers with 1/30 of a second also and that works fine.
Jack
Well, count yourself lucky. The docs seem to indicate that timers that quick shouldn't be relied on.
Dan Ray
BTW, the same paragraph of the docs explains: `A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed.` You are reading the docs, right?
Dan Ray
+1  A: 

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.

hotpaw2
Jack