2016-12-29 1 views
0

일요일에 사용자가 일요일을 잊어 버렸을 때 월요일 또는 화요일 또는 한 달에 애플리케이션을 열면 데이터가 새로 고쳐 져야하는 매주 일요일에 데이터를 새로 고치는 방법.일요일마다 데이터를 새로 고치는 방법

-(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; 
    } 
} 

초기 단계의 내 시작 날짜가 목요일에하고 있기 때문에하지 상쾌한 나는 일요일 건너 뛰고 직접 데이터를 새로 고침하지 월요일에 갈 때.

+0

어떻게하면 'refreshDate' 또는'noOfDaysCount'를 설정하는지 표시하지 않습니다. NSUserDefaults에 플래그를 저장하여 이전에 앱이 실행되었는지 확인할 수 있습니다. 그렇지 않은 경우 시간에 관계없이 새로 고침을 수행합니다. 그것이 당신이 요구하는 것이 아니라면, 더 많은 정보/코드가 다른 사람들이 대답 할 수 있도록 도움이 될 것이라고 생각하십시오. – Flexicoder

+0

일요일의 배열을 생성하고 그 날짜를 정기적으로 확인하십시오. 한 날짜가 전달/일치되면 데이터 세트를 업데이트하고 해당 날짜를 배열에서 제거합니다. –

답변

0

당신이 할 수 만 두 가지 도구가 필요합니다 .... 가장 최근의 일요일을 대표하는 (1) 생성 능력있는 NSDate를

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSCalendarOptions options = NSCalendarMatchPreviousTimePreservingSmallerUnits | NSCalendarSearchBackwards; 
NSDate *mostRecentSunday = [calendar nextDateAfterDate:[NSDate date] matchingUnit:NSCalendarUnitWeekday value:1 options:options]; 

새로 고침이 수행 마지막 때 (2) 지식, 앱에서 깨어 때마다 뭔가 등 ...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSDate *mostRecentRefresh = [defaults objectForKey:@"mostRecentRefresh"]; 

절차, 가장 최근의 일요일, 가장 최근 새로 고침 날짜를 얻는 것입니다. 가장 최근의 일요일이 나중 날짜 인 경우 새로 고침을 수행하고 현재 날짜를 마지막 새로 고침 날짜로 기록하십시오. 간단히 ...

if (mostRecentSunday == [mostRecentSunday laterDate:mostRecentRefresh]) { 
    // do refresh 
    [defaults setObject:[NSDate date] forKey:@"mostRecentRefresh"]; 
} 
+0

의견 보내기 : 매주 일요일마다 데이터가 새로 고쳐집니다. 새로운 문제는 일요일에 놓친 애플리케이션을 열 때 다음에 데이터를 새로 고쳐야하거나 일요일에 놓친 애플리케이션을 열어야한다는 것입니다. – kiran

+0

정말입니까? 내가 제안한 조건은 새로 고침 후 일요일 또는 그 이후에 앱을 열 때마다 true입니다. – danh

관련 문제