I want to develop a system for restaurant, which want to calculate time remaining in order(like 5 mints remaining in order of pizza),
I want to know what basic code be for this cou开发者_如何学Pythonnter, like how can I use NSDate or any other class???
Please guide
Get amount of seconds since a certain NSDate:
[[NSDate date] timeIntervalSinceDate:startingDate];
[NSDate date] returns the current NSDate object, and thats equal to the current date and time.
I think you can implement a recurring timer, paced each 30 seconds (don't do it too fast), that will poll all pending requests and update the countdown for each order and will signal you all orders that reached the countdown.
E.g.: let's say you take a 5 minutes order. Then you will calculate the date the order will be ready by adding 5*60 seconds to the current date:
NSDate *orderReadyAt = [NSDate dateWithTimeIntervalSinceNow:5*60];
Then you will store this date to your order end date property. E.g.:
Order *myOrder = [[Order alloc] init];
... make your order here ...
myOrder.readyDate = orderReadyAt;
At application startup you will define a recurring timer that will trigger each 30 seconds and will call the "countdownCheck" function:
[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(countdownCheck) userInfo:nil repeats:YES];
At this point what you have to do in your countdownCheck is to loop through your Order instances and for each of them calculate the time remaining:
-(void)countdownCheck {
for(Order *aOrder in allOrders) {
NSTimeInterval secondsRemaining = [aOrder.readyDate timeIntervalSinceNow];
//... do something: update your GUI, show an alert if secondsRemaining<0...
}
}
Clearly this approach is not time critical, the only requirement you have is to run the code inside the countdownCheck in the 30 seconds limit, which is of course more than enough. You can ideally set it to 1 second only, provided your loop is fast enough.
精彩评论