2014-01-23 2 views
0

우리의 앱에서는 NSCalendar를 사용하여 NSDate 객체를 처리합니다. 예를 들어 요일 구성 요소를 가져 오는 중입니다.감지 [NSTimeZone localTimezone] secondsFromGMT 속성 변경

이 NSCalendar 인스턴스는 단시간에 많은 계산을하기 때문에 싱글 톤 객체입니다. 우리가 필요로 할 때마다 [[NSCalendar alloc] initWith ...]를 사용하여 새 인스턴스를 생성하는 것으로 나타났습니다. 많은 시간.

문제는 응용 프로그램 실행 시간 동안 NSCalendar 인스턴스를 유지하면이 인스턴스는 표준 시간대가 변경된 경우에도 변경되지 않은 상태로 유지됩니다. 우리가 논의하고

self.singletonCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] 

또는

self.singletonCalendar.timezone = [NSTimezone localTimezone]; 

가능한 해결책 :

그래서, 문제는 우리가 시간대 변경을 감지하고 싶은 중 하나에 의해 적절하게 반응한다는 것이다

  1. 각 시간대의 시간대 속성 업데이트 응용 프로그램이 활성화됩니다.
  2. KVO를 사용하여 시간대 변경 사항을 감지합니다 (성공하지 못한 경우이를 시도 함).

감사합니다. Nicolás. Putz1103에 의해


구현 제안 된 솔루션 :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeNotification) name:UIApplicationSignificantTimeChangeNotification object:nil]; 

- (void)significantTimeChangeNotification 
{ 
    NSTimeZone *localTimezone = [NSTimeZone localTimeZone]; 
    [KIDateUtils setCalendarTimezone:localTimezone]; 
} 
+0

업데이트시 제안 된 솔루션을 Putz1103에 게시했지만 rmaddy가 제공 한 답변입니다. Putz1103은'UIApplicaitonDelegate' 메소드'applicationSignificantTimeChange :'를 사용하여 제안했습니다. – rmaddy

답변

2

내가 두 가지 옵션의 매시업을 할 것입니다. 나는 앱 시작시 (또는 포 그라운드로) 업데이트 할 것이다. 그러나 앱이 전경에 현재있는 동안 당신은 answer here 같은 시간 변경에 대한 리스너를 설정할 수 있습니다

"UIApplicationDelegate" has a method called "applicationSignificantTimeChange:" that gets called when there is a significant change in the time.

2

UIApplicationSignificantTimeChangeNotification 통지에 대한 단일 레지스터 되세요. 이 메시지를 받으면 싱글 톤의 NSCalendar 인스턴스를 업데이트하십시오.