Reputation: 1
For three months I am unable to finish this work. I need help. I am stuck and wasting time. I am having difficult time explaining this. Anyways, I have this code right here.
NSDateFormatter *detailsTimeFormatter = [[NSDateFormatter alloc] init];
[detailsTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%@",[[detailsTimeFormatter stringFromDate:[NSDate date]] lowercaseString]);
//if (INSERT CODE HERE) { ///<-- Detect every Hours and Minutes
// NSLog(@"True");
//}else{
// NSLog(@"False");
//}
I need a code that can detect changes in every minutes.
I'm not sure how you do it. I like to give an example.
1:00 am -> NSLog(@"True");
1:00:10 am --> NSLog(@"False");
1:00:11 am --> NSLog(@"False");
1:00:12 am --> NSLog(@"False");
1:00:## am --> NSLog(@"False");
1:01 am --> NSLog(@"True");
1:01:01 am --> NSLog(@"False");
1:01:02 am --> NSLog(@"False");
1:01:03 am --> NSLog(@"False");
1:01:## am --> NSLog(@"False");
1:01:59 am --> NSLog(@"False");
1:02 am --> NSLog(@"True");
1:02:01 am --> NSLog(@"False");
1:02:02 am --> NSLog(@"False");
1:02:03 am --> NSLog(@"False");
1:02:## am --> NSLog(@"False");
1:02:59 am --> NSLog(@"False");
1:03 am --> NSLog(@"True");
Upvotes: 0
Views: 748
Reputation: 3346
If you need something with that exact structure, my answer probably won't help you at all. Else if you just need to execute a piece of code every minute, you can try using the NSTimer
class, this post is a great source for a description on how to use it.
All you would have to do is call - (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
(form the NSTimer class) and have it inited the way you want it. Hope this helps, good luck!
Edit:
Well you won't explicitly "detect minutes", a callback would respond every minute, in this callback you would have your code to do whatever you want to do. You just have to fire your method at a date in a minute with zero seconds.
Upvotes: 1
Reputation: 3921
It sounds like you are looking for a 'Real Time' mechanism. Even NSTimer's - even if you could get the timer to start on exactly a minute boundary - are not 'Real Time' they are still only approximate.
From NSTimer Doco: 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. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
That being said - you could kick off a timer with this method and it should start at a specific time and then refire every minute there after ('approximately as per above').
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats – gamozzii 1 min ago edit
For Example:
-(void)mySetupMethod {
NSDate *startTimeOnMinuteBoundary = ... create a date that starts on minute boundary using a date formatter ...
NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:startTimeOnMinuteBoundary target:self selector:@selector(myMinuteBoundaryFirer:) userInfo:nil repeats:TRUE;
}
-(void) myMinuteBoundaryFirer:(NSTimer *)theTimer {
NSLog(@"This should be on a minute boundary, approximately only though because this is not a real time timer");
}
Upvotes: 1