미해결
배달앱 클론코딩 [with React Native]
[iOS] RCTTMap.m requiresMainQueueSetup 경고 관련
우여곡절 끝에 티맵 iOS 설정을 마치고 지도가 열리는 걸 보는 기쁨도 잠시 시뮬레이터, 아이폰 모두에서 나타하는 경고가 신경쓰이는군요.
열어보면 다행인지 불행인지 길고 자세한 경고문이 나옵니다.
RCTTMap.h를 보면
@interface RCTTMap : NSObject <RCTBridgeModule>
RCTBridgeModule을 상속하는 것 같은데 RCTBridgeModule.h을 열어보니 역시나 길고 자세한 설명이 써져 있더군요.
/**
* Most modules can be used from any thread. All of the modules exported non-sync method will be called on its
* methodQueue, and the module will be constructed lazily when its first invoked. Some modules have main need to access
* information that's main queue only (e.g. most UIKit classes). Since we don't want to dispatch synchronously to the
* main thread to this safely, we construct these modules and export their constants ahead-of-time.
*
* Note that when set to false, the module constructor will be called from any thread.
*
* This requirement is currently inferred by checking if the module has a custom initializer or if there's exported
* constants. In the future, we'll stop automatically inferring this and instead only rely on this method.
*/
대강 해석해보면 비동기 함수를 갖는 경우 등의 이유로 모듈은 아무 쓰레드에서나 작동하는데(자기 methodQueue에서 called되는 게 기본동작), 어떤 모듈은 메인쓰레드(UIKit)의 정보에 접근할 필요가 있다는 것이고, 그럴 때는 requiresMainQueueSetup을 true(YES)로 명시적으로 지시해야 한다.. 지금은 custom init 이 있는지 등을 보고 RN이 유추해주지만 미래엔 유추를 중단할테니 requiresMainQueueSetup을 명시적으로 선언해라... 뭐 그런 내용 같네요.
좋은 내용 같긴 한데.. 티맵 모듈의 경우 만약 우리 배달앱 내에 지도를 랜더링한다면 당연히 메인쓰레드에서 동작하게 해야할텐데, 강의 방향을 보면 단순히 티맵을 열게 호출하는 기능만 하기 때문에 굳이 메인쓰레드여야 할 필요가 없는 것 아닌가 싶기도 합니다. 어떤 쪽이 맞을까요?
단순히 경고만 안 뜨게 하는 것은 쉬웠습니다. RCTTMap.m에
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
만 넣어주면 경고도 없어지고 티맵 작동에도 지장이 없습니다. 그런데 문제는!
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
라고 넣어줘도 경고도 없어지고 티맵도 정상 작동한다는 거죠.
openNavi() 메소드는 네이버지도 위 Marker나 Path에 대한 터치 이벤트를 핸들러로 작용하니 당연히 UI가 돌아가는 메인쓰레드에서 호출되도록 고정(return YES)해야만 하고, 백그라운드 쓰레드에서 호출되면(return NO) 오류가 나야 정상일 것 같은데 말이죠. 이유를 설명해주실 수 있나요?
선생님 교안에서도 '메인쓰레드만 쓰도록'이라고 코멘트 해놓으셨는데
- (dispatch_queue_t)methodQueue
{
// 메인쓰레드만 쓰도록
return dispatch_get_main_queue();
}
이 코드에 대해서도 설명 부탁드립니다.