2017-01-02 1 views
0

일요일을 확인하고 데이터를 재설정하는 방법은?일요일과 휴식 데이터를 확인하는 방법

매주 일요일에 재설정해야합니다. 일요일에 알림을 놓친 경우 사용자가 다음 증분 날짜 (예 : 월요일, 화요일, 토요일까지)를 열면 재설정해야합니다.

매주 일요일 또는 일요일을 넘었을 때 사용자가 휴식 날짜에 응용 프로그램을 여는 것을 잊었을 때 재설정해야합니다.

구현하려고하는 내 코드를 따르십시오. 내 요구 사항에 따라 작동하지 않았던 귀하의 의견은 높이 평가됩니다.

 -(void) weekDaySundayToRefresh 
     { 
      NSDate *now = [NSDate date]; 
      NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
      NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekday fromDate:now]; 

      NSUInteger weekdayToday = [components weekday]; 
      NSInteger daysToMonday = (8 - weekdayToday) % 7; 

      nextSunday= [now dateByAddingTimeInterval:60*60*24*daysToSunday]; 

      NSLog(@"nextMonday : %@", nextSunday); 
     } 

     -(void) compareDate 
     { 
      NSDate *today = [NSDate date]; // it will give you current date 
      // NSDate *newDate = [MH_USERDEFAULTS objectForKey:@"USER_BACKGROUND_DATE"]; // your date 

      NSComparisonResult result; 
      //has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending 

      result = [today compare:refreshGoalsDate]; // comparing two dates 

      if(result==NSOrderedAscending) 
      { 
       NSLog(@"today is less"); 
      } 
      else if(result==NSOrderedDescending) 
      { 
       NSLog(@"newDate is less"); 

       NSDate *fromDate; 
       NSDate *toDate; 

       NSCalendar *calendar = [NSCalendar currentCalendar]; 

       [calendar rangeOfUnit:NSCalendarUnitDay startDate:&fromDate 
          interval:NULL forDate:refreshGoalsDate]; 
       [calendar rangeOfUnit:NSCalendarUnitDay startDate:&toDate 
          interval:NULL forDate:today]; 

       NSDateComponents *difference = [calendar components:NSCalendarUnitDay 
                  fromDate:fromDate toDate:toDate options:0]; 

       noOfDaysCount = [difference day]; 
       NSLog(@"date difference:%ld",(long)[difference day]); 
      } 
      else 
       NSLog(@"Both dates are same"); 
     } 

    -(void) checkRefreshGoals:(int)sectionIndex rowIndex:(int)rowIndex 
    { 
     NSDateFormatter * formatter = [NSDateFormatter new]; 
     [formatter setDateFormat:@"yyyy-MM-dd"]; 
     NSString * currentDateString = [formatter stringFromDate:[NSDate date]]; 
     NSString * initialNotificationDate = [formatter stringFromDate:refreshDate]; 
     NSDate *currentDate = [formatter dateFromString:currentDateString]; 
     NSDate *refreshDate = [formatter dateFromString:initialNotificationDate]; 

     if ([refreshDate compare:currentDate] == NSOrderedDescending) 
     { 
      NSLog(@"date1 is later than date2"); 
      isGoalsRefreshed = NO; 
     } 
     else if ([refreshDate compare:currentDate] == NSOrderedAscending) 
     { 
      NSLog(@"date1 is earlier than date2"); 
      if (sameDay == YES) 
      { 
       isGoalsRefreshed = YES; 
       [self refreshDataOnSunday:sectionIndex rowIndex:rowIndex]; 
      } 
      else if (noOfDaysCount > 7) 
      { 
       isGoalsRefreshed = YES; 
       [self refreshDataOnSunday:sectionIndex rowIndex:rowIndex]; 
      } 
      else 
      { 
       isGoalsRefreshed = NO; 
      } 
     } 
     else 
     { 
      NSLog(@"dates are the same"); 
      isRefreshed = NO; 
     } 
    } 

-(void) refreshDataOnSunday:(int)sectionIndex rowIndex:(int)rowIndex 
{ 
    if (noOfDaysCount > 7) 
    { 
     if (isGoalsRefreshed == YES) 
     { 

      // Do rest your data 


     } 
    } 
    else if (sameDay == YES) 
    { 
     if (isGoalsRefreshed == YES) 
     { 
     // Do reset your data 

     } 
    } 
    else 
    { 
     isGoalsRefreshed = NO; 
    } 
} 
+0

최근 업데이트 된 시간을 저장 하시겠습니까? – karthikeyan

답변

0

먼저 당신은 사용자가 처음에 당신의 응용 프로그램을 열고 다음 당신은 당신이 추정 할 수 있습니다 일요일 사라이나되지 않도록 다음 응용 프로그램 시작 날짜를 비교해야 할 때 오늘 날짜와 DayNumber을 저장해야합니다.

/*first check in userDefault for stored Date and Day 
Store values in local and check with current date */ 

NSMutableDictionary *dictStoredData = [USERDEFAULT valueForKey:@"dateTimeDate"]; 

if (!dictStoredData) { 
    // user comes first time so store it here 
    dictStoredData =[[NSMutableDictionary alloc] init]; 

    NSDate *today =[NSDate date]; 
    NSCalendar *gregorian =[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
    NSDateComponents *comps =[gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]]; 

    NSInteger weekday = [comps weekday]; 
    int nextSundayVal = 7 - (int)weekday; 

    NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
    [format setDateFormat:@"yyyy-MM-dd"]; 
    NSString *startDate = [format stringFromDate: today]; 
    [dictStoredData setObject:startDate forKey:@"startDate"]; 
    [dictStoredData setObject:[NSString stringWithFormat:@"%d",nextSundayVal] forKey:@"nextSundayVal"]; 

    [USERDEFAULT setObject:dictStoredData forKey:@"dateTimeDate"]; 
} 
else{ 
     NSString *strEndDate = @"2017-01-08"; 
     NSString *strStartDate = [dictStoredData valueForKey:@"startDate"]; 
     int nextSundayVal = (int)[dictStoredData valueForKey:@"nextSundayVal"]; 
     NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"yyyy-MM-dd"]; 
     NSDate *startDate = [format dateFromString:strStartDate]; 
     NSDate *endDate = [format dateFromString:strEndDate]; 

     NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
     NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay 
                  fromDate:startDate 
                   toDate:endDate 
                  options:0]; 

     int newDayCount = (int)[components day]; 

     if (newDayCount > nextSundayVal) { 
      NSLog(@"sunday is gone so reload data!!"); 
      //save this date as a new date and check with this when user come again 
     } 
     else { 
      NSLog(@"sunday is not come yet!!"); 
     } 

} 

여기 strEndDate는 같은 dateformater와 [NSDate date]에서 그것을 얻을 수 있도록 사용자가 응용 프로그램을 열 때 다음 날짜입니다. 내 컴퓨터에서이 코드를 실행했으며 인쇄가 일요일에 끝났으므로 데이터이 다시로드되므로 도움이되기를 바랍니다.

0

우리와 마찬가지로, 우리는 모든 금요일과 7 일 이상, 서버를 업데이트해야합니다. 다음 코드를 사용했습니다. 마지막으로 업데이트 한 날짜를 업데이트해야합니다. 체크 아웃 :

 if ([isFirday isEqualToString:@"Friday"]) {//This is my value from server side (they will give as friay), i think you dont need this condition. 
      BOOL isSameDay; 
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
      [dateFormatter setDateFormat:@"MMM dd yyyy,HH:mm"]; 
      NSDate *date = [dateFormatter dateFromString:mennuRefreshTime];//Is an last updated date. 
      [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
      NSString *temp = [dateFormatter stringFromDate:date]; 
      NSDate *lastDate = [dateFormatter dateFromString:temp]; 

      NSString *currentDaystr = [dateFormatter stringFromDate:[NSDate date]]; 
      // Write the date back out using the same format 
      NSDate *currentDate = [dateFormatter dateFromString:currentDaystr]; 

      if ([lastDate compare:currentDate] == NSOrderedDescending) { 
       NSLog(@"date1 is later than date2"); 
       isSameDay = NO; 
      } else if ([lastDate compare:currentDate] == NSOrderedAscending) { 
       NSLog(@"date1 is earlier than date2"); 
       [dateFormatter setDateFormat:@"EEEE"]; 
       NSString *dayName = [dateFormatter stringFromDate:currentDate]; 
       NSString *lastupdateName = [dateFormatter stringFromDate:lastDate]; 

       if ([dayName isEqualToString:lastupdateName]) { 
        isSameDay = YES; 
       }else{ 
        isSameDay = NO; 
       } 

      } else { 
       NSLog(@"dates are the same"); 
       isSameDay = YES; 
      } 
      if (isSameDay == YES) { 
       return; 
      } 


      NSCalendar *calender =[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
      NSDateComponents *components = [calender components:NSCalendarUnitDay fromDate:lastDate toDate:currentDate options:0]; 

      int day = (int)[components day]; 
      if (day >= 7) { 
       [self getTopList]; 
      }else{ 
       [dateFormatter setDateFormat:@"EEEE"]; 
       NSString *dayName = [dateFormatter stringFromDate:currentDate]; 
       if ([dayName isEqualToString:isFirday]) { 
        [self getTopList]; 
       }else{ 
        return; 
       } 
      } 


     } 
관련 문제