NotificationCenter And Block

作者: Life_小贤 | 来源:发表于2017-05-04 10:26 被阅读33次

    最近有朋友问通知的底层怎么实现,然后我大致的理了下:
    1.通知是单例来的
    2.通知可以注册与注销,说明里面有数组在控制着
    3.通知是要实现方法的,说明里面有协议存在
    4.通知是有设置通知接收者的,说明这是使用了代理的

    综合上述说明,我自己基于blcok写了套自己的通知:
    主要是使用两个方法

    // 接收通知
    + (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key;
    
    // 发送通知
    + (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message;
    
    

    先在需要接收的地方添加上block,然后在需要传值出来的地方发送通知(信息通过message来传递)

    使用方法
    先对应key添加blcok

     [NotificationCenterBlock addBlocksObject:^(NSDictionary *info) {
            NSLog(@"%s -- %@",__func__,info);
        } WithKey:@"zzz"];
    

    然后再对应key发送消息

     [NotificationCenterBlock sendNotificationWithKey:@"zzz" Info:@{@"JAJAAA":@"SSDDDDD",@"JAJAAAss":@"SSDDDDDss"}];
    

    其中的key就是接收和发送的识别钥匙,也就是对应key发送与添加block,同时这里的block是可以添加多个的。

    下面是这个通知类的代码

    NotificationCenterBlock.h

    //
    //  NotificationCenterBlock.h
    //  ZZZ
    //
    //  Created by zxw on 17/5/4.
    //  Copyright © 2017年 tianshikechuang. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    typedef void(^NotifBlock)(NSDictionary * info);
    
    @interface NotificationCenterBlock : NSObject
    
    // 发送通知
    + (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message;
    
    // 接收通知
    + (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key;
    
    
    
    + (instancetype)shareNotificationCenterBlockWithKey:(NSString *)key;
    + (instancetype)NotificationCenterBlockWithKey:(NSString *)key;
    - (instancetype)initNotificationCenterBlockWithKey:(NSString *)key;
    
    - (void)sendNotificationInfo:(NSDictionary *)message;
    
    - (void)addBlocksObject:(NotifBlock)object;
    - (void)removeAllBlocksObject;
    
    
    + (void)removeAllBlocksObjectWithKey:(NSString *)key;
    
    @end
    
    

    NotificationCenterBlock.m

    //
    //  NotificationCenterBlock.m
    //  ZZZ
    //
    //  Created by zxw on 17/5/4.
    //  Copyright © 2017年 tianshikechuang. All rights reserved.
    //
    
    #import "NotificationCenterBlock.h"
    
    
    @interface NotificationCenterBlock ()
    
    @property(nonatomic, strong) NSString * key;
    @property(nonatomic, strong) NSMutableDictionary * keyBlockDic;
    
    @end
    
    
    
    @implementation NotificationCenterBlock
    
    /* --- 单例创建 --- */
    static NotificationCenterBlock *notif = nil;
    - (instancetype)init{
        
        if (notif) {
            return notif;
        }
        
        if (self = [super init]) {
            self.keyBlockDic = [NSMutableDictionary dictionary];
            notif = self;
        }
        return self;
    }
    
    
    - (instancetype)initNotificationCenterBlockWithKey:(NSString *)key{
        self = [[NotificationCenterBlock alloc] init];
        self.key = key;
        return self;
    }
    
    + (instancetype)shareNotificationCenterBlockWithKey:(NSString *)key{
        return [NotificationCenterBlock NotificationCenterBlockWithKey:key];
    }
    
    + (instancetype)NotificationCenterBlockWithKey:(NSString *)key{
        return [[NotificationCenterBlock alloc] initNotificationCenterBlockWithKey:key];
    }
    
    
    
    
    /* --- 发送通知 --- */
    + (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message{
        
        NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
        [notif sendNotificationInfo:message];
    
    }
    
    - (void)sendNotificationInfo:(NSDictionary *)message{
        NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
        if (!arr) {
            return;
        }
        for (NotifBlock blcok in arr) {
            blcok(message);
        }
        
        self.keyBlockDic[self.key] = arr;
    }
    
    
    
    
    
    /* --- 添加和移除 --- */
    + (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key{
         NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
        [notif addBlocksObject:object];
    }
    + (void)removeAllBlocksObjectWithKey:(NSString *)key{
        NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
        [notif removeAllBlocksObject];
    }
    
    
    - (void)addBlocksObject:(__autoreleasing NotifBlock)object{
        
        if (!object) return;
        
        NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
        if (!arr) {
            arr = [NSMutableArray array];
        }
        [arr addObject:object];
        
        self.keyBlockDic[self.key] = arr;
        
    }
    
    - (void)removeAllBlocksObject{
        
        NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
        if (arr) {
            [arr removeAllObjects];
        }
        
        self.keyBlockDic[self.key] = arr;
    }
    
    
    @end
    
    

    如果有什么地方有漏洞希望大家及时指出,以便及时更改!

    相关文章

      网友评论

        本文标题:NotificationCenter And Block

        本文链接:https://www.haomeiwen.com/subject/ghertxtx.html