123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- //
- // SPSBNotificationDelegateManager.m
- // 我的社保
- //
- // Created by shanp on 2021/4/20.
- //
- #import "SPSBNotificationDelegateManager.h"
- #import <JXHMacro.h>
- @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<SPSBNotificationDelegate>)delegate keys:(NSArray<SPSBNotificationDelegateKey> *)keys {
- for (SPSBNotificationDelegateKey key in keys) {
- [self setDelegate:delegate key:key];
- }
- }
- - (void)removeDelegate:(id<SPSBNotificationDelegate>)delegate keys:(NSArray<SPSBNotificationDelegateKey> *)keys {
- for (SPSBNotificationDelegateKey key in keys) {
- [self removeDelegate:delegate key:key];
- }
- }
- - (void)setDelegate:(id<SPSBNotificationDelegate>)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<SPSBNotificationDelegate>)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];
- }
|