Reputation: 805
I need to start download exactly one hour before or within my custom time. For example I should start to download at 7 AM or with 8 AM (8 AM is my custom time). How I can achieve this. the following is my code,
AppDelegate * iDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
iDelegate.mStartTime = [[NSUserDefaults standardUserDefaults] objectForKey:START_TIME];
int stHr = [[[iDelegate.mStartTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
int stMin = [[[iDelegate.mStartTime componentsSeparatedByString:@":"] objectAtIndex:1] intValue];
int formStTime = (stHr*60)+stMin;
NSDate *today = [[[NSDate alloc] init]autorelease];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init]autorelease];
[offsetComponents setHour:+1];
NSDate *modifiedDate = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
NSDateComponents * timecomponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:modifiedDate];
NSInteger currentHour = [timecomponents hour];
NSInteger currtMin = [timecomponents minute];
int nowTime = (currentHour*60)+currtMin;
if (nowTime <= formStTime || nowTime >= formStTime)
{
// start download
}
But I should not download if the current time is 8 AM or after 8 AM . What is the mistake in my code ? Please suggest. Thanks.
Upvotes: 1
Views: 84
Reputation: 69047
I have not considered in detail all your code's logic, but this struck to me:
if (nowTime <= formStTime || nowTime >= formStTime)
this condition will be always true. This explains why your code is not working as you wish since it should always do the downloading, even after 8AM. You are after a condition link this:
if (nowTime >= minimumTime || nowTime < maximumTime)
However, I have not clear from your question how you define minimum
and maximumTime
so cannot advise further. If formStTime
is your minimumTime
and we assume that maximumTime
is just one hour later, you could have:
if (nowTime >= formStTime || nowTime < formStTime + 60)
(If I read correctly your code.)
EDIT: after your clarification in the comments, the condition to start the download would be:
if (nowTime < formStTime)
//-- start download
To handle the second part of your requirements (stopping the download at the given maximum time) you will need a different logic altogether. One way of doing it would be starting a NSTimer
when you start a download that will fire at the right time; then, you would check if the download is still going on and stop it. A NSTimer
is just one of multiple possibilities you have (you could also use displatch_after
), but the basic idea will not change.
E.g.:
if (nowTime < formStTime) {
//--= start download
self.timer = [NSTimer scheduledTimerWithTimeInterval:(formStTime - nowTime)*60 //-- seconds
target:self
selector:@selector(timeExpired:)
userInfo:nil
repeats:NO];
}
then in the handler function:
- (void)timeExpired:(NSTimer*)timer {
if (downloadStillOngoing)
//-- stop the download
}
When your download is done, you should call [self.timer invalidate]
, also.
However, you will need to fill a number of gaps in my sketch implementation to get to a working solution. If you need more help about stopping the download, you will need to provide more information about how you are doing the download and possibly ask a new question.
Upvotes: 4
Reputation: 17186
Modify your if condition with below code:
if (nowTime >= formStTime && nowTime <= formStTime + 60)
Upvotes: 2