1
주 스레드의 모든 알림을 게시해야하는 알림 관리자 클래스가 있습니다. 하지만 충돌 보고서가 있습니다. 다음 줄에서 앱이 다운됩니다.주 스레드 충돌시 NSNotification 게시
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:aName object:anObject];
이 문제를 해결하는 방법에 대한 조언을 받으려합니다. 사전에 들으
#import "NotificationManager.h"
@interface NotificationManager()
{
NSNotificationCenter *center;
}
@end
@implementation NotificationManager
+ (NotificationManager *) sharedInstance
{
static NotificationManager *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[NotificationManager alloc] init];
});
return instance;
}
- (void) postNotificationName:(NSString *)aName object:(id)anObject
{
if (dispatch_get_current_queue() != dispatch_get_main_queue())
{
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:aName object:anObject];
});
}
else
{
[[NSNotificationCenter defaultCenter] postNotificationName:aName object:anObject];
}
}
@end
충돌 보고서 :
7 PLR 0x000e5d40 __51-[NotificationManager postNotificationName:object:]_block_invoke in NotificationManager.m on Line 27
8 libdispatch.dylib 0x39a14a88 _dispatch_barrier_sync_f_slow_invoke
9 libdispatch.dylib 0x39a105da _dispatch_client_callout
10 libdispatch.dylib 0x39a13e44 _dispatch_main_queue_callback_4CF
11 CoreFoundation 0x318cf1b0 __CFRunLoopRun
12 CoreFoundation 0x3184223c CFRunLoopRunSpecific
13 CoreFoundation 0x318420c8 CFRunLoopRunInMode
14 GraphicsServices 0x3542133a GSEventRunModal
15 UIKit 0x3375e2b8 UIApplicationMain
16 PLR 0x000c5b4a main in main.m on Line 9
내 질문에 오류 보고서 정보를 추가했습니다. – iWheelBuy
[dispatch_get_current_queue] (https://developer.apple.com/library/ios/DOCUMENTATION/Performance/Reference/GCD_libdispat) ch_Ref/Reference/reference.html # // apple_ref/c/func/dispatch_get_current_queue)은 iOS 6에서 사용되지 않습니다. 조건부 및 기본 큐로의 디스패치는 항상 제거해야합니다. –
'[NSThread isMainThread]'를 사용하면 메인 스레드인지 알 수 있습니다. –