Is there a way to accurately (to thousands of a second) delay the time between waiting for a view to appear waitForViewWithAccessibilityLabel and then tapping another view tapViewWithAccessibilityLabel in KIF. In my app code I set to DateTime objects on view appear and tap but this time difference does not match the delay I put in KIF.
I have tried waitForTimeInterval and also
while(true) {
NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:dateStart];
if(time > 1.678)
break;
}
but those both have errors of about 0.15 seconds. I assume that this error comes from the waitForViewWithAccessibilityLabel looking for the view to appear. Is there any way to set the start time of a the timer at the time the view is actually found? Or any other suggestions on solving this problem?
Cheers,
Mo
Related
I want to allow the user to enter some notes in a UITextView before closing an app. But he/she might not enter anything or he/she might enter some text and then stop and do nothing for a long time. In either of those cases I want to close the UITextView, save some stuff, and exit the app. How do I detect that the user didn't entered anything for a certain number of seconds?
I tried to dispatch a function after a wait time
static int idleSeconds = 8;
self.noteDoneTime = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * idleSeconds);
dispatch_after(self.noteDoneTime, dispatch_get_main_queue(), ^{ [self endNoteRequest]; });
but every time the user changes the text renew the dispatch time:
- (void)textViewDidChange:(UITextView *)textView {
if (textView == self.noteView) {
self.noteDoneTime = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * idleSeconds);
}
}
But that doesn't work because the dispatch time doesn't get updated after it is initially set, so the method 'endNoteRequest' runs even though the user is continuing to edit the text. I think I need to cancel the dispatch request and issue a new one but how can I do that?
Or is there some other approach to an idle timeout that actually works?
I do something just like this in a game app, where I penalize the user for every 10 seconds that elapses without the user's making any game move. It's like a "shot clock" in basketball, resetting after every shot.
It's easiest to do this with an NSTimer, because now you have an object that you can keep a reference to (e.g. an instance property). When the user types something, you invalidate the timer and set your reference to nil. Now it's gone! Now make a new one and set the reference to it. And so on.
In an iOS app I have a view with a UIDatePicker which only selects time (hour, minute, and am/pm). Then based upon the time of the UIDatePicker, sets an NSTimer. This timer is supposed to launch daily at the time specified on the UIDatePicker. Is there anyway to set up an NSTimer with a specified hour, minute, then either am or pm without having to take the date into consideration? Being able to do this would allow me to remove a huge amount of conditional statements regarding month's, day's and years.
If this isn't possible is there a recommended approach?
Thanks!
Check out UILocalNotification:
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/
Now I am creating a metronome program. I use a NSTimer to bring the metronome into play. Of course The Timer works repeatedly. But I find out in two situation that the timer works not accruately.
When just start the NSTimer, the first two beat sometimes goes too closely. After then, the beat goes evenly.
When the app goes backgound I make the Timer work continuely by:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
but sometimes the unevenly beat also happens at the time when entering or coming back from the background status.
So I want to know how to keep the Timer always work evenly, no matter which situation it is in. Thks!
When setting up a timer firing at a certain time, the system implicitly sets an allowed "leeway" which is a short delay within the timer may actually fire.
This is due to "Timer Coalescing" - a system feature - which groups events happening roughly at the same time together and fire them at the exact same time. The reason is to reduce CPU cycles and extend the idle time of the CPU to save power.
If the timer interval is small (milli seconds to seconds) this "leeway" is in the range of 10% of the timer interval.
You can explicitly retrieve and set the current maximum allowed leeway value with the methods
-tolerance
-setTolerance:
See also:
NSTimer
Timing Accuracy
I need to use a timer in my WP7 application and display it's value in the UI.
I managed to get it working two ways: one using the DispatcherTimer class and the other using the Timer class.
The problem is that the DispatcherTimer is slower than "real-time" and the Timer class actually goes faster than "real-time". So both go wrong.
Is this because I'm running it in the emulator? Don't have a device to test it on. Anyone had this issue and tested on both?
To show time accurate to one second on screen, I use a DispatcherTimer that updates every 500 milliseconds and then subtract the current time from the start time to get the time elapsed and display that on-screen.
Does that help?
My app draws a timer (with detail to .1 seconds), for which I am currently using a NSTime which fires every .1 seconds. This feels like an absolutely terrible idea, but I'm not sure how else to do it. I don't really care about the .1 seconds updating always, but I would like it to update more than once per second. Is there a good way to do this?
NSTimer doesn't strike me as a bad approach. NSTimer is generally a very regular way to keep track of time (indeed it was used for animation timing before CADisplayLink came along). Unless you are seeing unacceptable performance of your timer display updating, I would stick with this approach.
If you are having issues with delays and inaccurate time readings, you could store the start time in NSDate, and continue to use the NSTimer but only to update the display. On each timer event firing, you then update the display by finding the NSTimeInterval from the start time to now. This way even if there is a performance issue, at least the time being display should remain accurate at the time of display.