2016-07-19 1 views
-1

나는 Apple doc을 살펴보고 같은 스레드에 알림과 관찰자가 있어야한다는 것을 이해해야하지만 Apple 스레드 코어 데이터 예제 파서 작업이 주 스레드 이외의 스레드에서 알림을 보내고 있지만 관찰자는 주 스레드에 있다는 것을 알게되었습니다. 나는 그것에 대해 혼란스러워하거나 여기서 뭔가를 놓치고 있습니다.관찰자는 iOS의 NSNotification에서 다른 스레드를 청취 할 수 있습니까?

+0

APLParseOperation.m에 두 개의 알림 알림이 있습니다. 어느 것을 언급하고 있습니까? – jp2g

답변

0

ThreadedCoreData 샘플의 APLParseOperation.m에있는 두 가지 알림 호출 중 하나를 언급한다고 가정합니다.

관련 비트는 :

통지 1

[[NSNotificationCenter defaultCenter] postNotificationName:APLParseOperation.AddEarthQuakesNotificationName 
                object:self 
               userInfo:@{APLParseOperation.EarthquakeResultsKey: earthquakes}]; 

이것이 대한 관찰자 :

// In APLEarthQuakeSource.m 
_addEarthQuakesObserver = [[NSNotificationCenter defaultCenter] addObserverForName:APLParseOperation.AddEarthQuakesNotificationName 
                      object:nil 
                      queue:nil 
                     usingBlock:^(NSNotification *notification) { 
    /** 

이 때문에 queue 대한 nil 파라미터 미세하다. docs에 따르면이 매개 변수에 nil을 전달하면 "블록이 게시 스레드에서 동 기적으로 실행됩니다." 문제 없다.

알림 # 2

다음 통지,

항상 주 스레드에서 호출되는
- (void)handleEarthquakesError:(NSError *)parseError { 

    assert([NSThread isMainThread]); 
    [[NSNotificationCenter defaultCenter] postNotificationName:APLParseOperation.EarthquakesErrorNotificationName 
                 object:self 
                 userInfo:@{APLParseOperation.EarthquakesMessageErrorKey: parseError}]; 
} 

:

[self performSelectorOnMainThread:@selector(handleEarthquakesError:) withObject:parseError waitUntilDone:NO]; 

'희망이 도움이됩니다.

+0

늦게 답장을 드려 죄송합니다. 나는 현재 스레드에서 동 기적으로 실행 스레드를 만들 것입니다 그래서 우리는 같은 스레드에있는 것처럼 의미를 만들 nil 매개 변수를 전달할 수없는 것 같아요. 나 맞아? – manismku

+0

@manismku : 예, 그게 전부입니다. '도움이 된 것을 기쁘게 생각합니다. 행복하다면 대답을 수락하십시오. – jp2g

+0

감사합니다. 나는 당신의 대답을 받아 들였지만, 새로운 것이기 때문에 점수는 공개적으로 표시 할 수 없습니다. – manismku

관련 문제