I’m testing a few things for my self in OS X, and I would like to know what the best way to do this would be.
I have a method, that returns the current week number.
-(NSInteger) getWeekNumber {
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:date];
return [components week];
}
Since this will only happen once a week it seems stupid to have a thread that will refresh my label once every second or so, but I want the label value to change at the exact second the week number change.
I also want to update a label with the current time, that will actually be once a second, or once a minute depending on settings. Should that just be a thread that runs once a second?
There is no need to use threads for simple tasks like these.
NSTimer is the way to go, do something like this for the weekly updates:
The following function will be triggered when the new week starts:
Use something like this for the clock:
Which triggers this function every second: