// // SPSBNotificationDelegateManager.m // 我的社保 // // Created by shanp on 2021/4/20. // #import "SPSBNotificationDelegateManager.h" #import @implementation SPSBNotificationDelegateManager + (instancetype)alloc { NSAssert(![self isMemberOfClass:[SPSBNotificationDelegateManager class]], @"SPSBNotificationDelegateManager is singleton, you should not instantiate it directly."); return [super alloc]; } + (SPSBNotificationDelegateManager *)shareManager { static SPSBNotificationDelegateManager *manager = nil; static dispatch_once_t once; dispatch_once(&once, ^{ manager = [[super allocWithZone:nil] init]; manager->_spsb_delegates = NSMutableDictionary.new; }); return manager; } + (instancetype)allocWithZone:(struct _NSZone *)zone { return [self shareManager]; } - (void)setDelegate:(id)delegate keys:(NSArray *)keys { for (SPSBNotificationDelegateKey key in keys) { [self setDelegate:delegate key:key]; } } - (void)removeDelegate:(id)delegate keys:(NSArray *)keys { for (SPSBNotificationDelegateKey key in keys) { [self removeDelegate:delegate key:key]; } } - (void)setDelegate:(id)delegate key:(SPSBNotificationDelegateKey)key { dispatch_main_async_safe(^{ if (!self->_spsb_delegates[key]) { self->_spsb_delegates[key] = [NSMapTable weakToStrongObjectsMapTable]; } [self->_spsb_delegates[key] setObject:key forKey:delegate]; }); } - (void)removeDelegate:(id)delegate key:(SPSBNotificationDelegateKey)key { dispatch_main_async_safe(^{ if (!self->_spsb_delegates[key]) return; [self->_spsb_delegates[key] removeObjectForKey:delegate]; if (self->_spsb_delegates[key].count == 0) { self->_spsb_delegates[key] = nil; } }); } @end SPSBNotificationDelegateManager *spsb_NotificationDelegateManager() { return [SPSBNotificationDelegateManager shareManager]; }