其他分享
首页 > 其他分享> > iOS奇思妙想之使用block替代通知(二)

iOS奇思妙想之使用block替代通知(二)

作者:互联网

前言

在之前的文章iOS奇思妙想之使用block替代通知(一)中,自己实现了通知功能。在之前的实现中,使用了Runtime进行动态绑定,虽然达到了最后的效果,但是也会增加耦合。

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的iOS交流群:196800191,加群密码:112233,不管你是小白还是大牛欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!

解决耦合

既然动态绑定属性会增加耦合,那么我们可以考虑不让观察者动态绑定属性来实现。这里参考之前NSMapTable,将对应属性存入,在调用的时候再根据不同的Key动态获取。
1.添加监听
根据不同的观察者和监听类型动态生成Key,使用NSMapTable将对应Key和观察者设置的属性储存。

+ (void)addObserver:(id)observer type:(CLActionType)type mainThread:(BOOL)mainThread actionBlock:(void(^)(id observer, NSDictionary *dictionary))actionBlock {
    dispatch_semaphore_wait([CLActionManager sharedManager].semaphore, DISPATCH_TIME_FOREVER);
    NSString *key = [NSString stringWithFormat:@"%@-%@",[NSString stringWithFormat:@"%p",observer], [[self keyWithActionType:type] stringByAppendingString:@"-1"]];
    NSString *actionBlockKey = [key stringByAppendingString:@"-CLActionBlock-1"];
    NSString *actionMainThreadKey = [key stringByAppendingString:@"-CLActionMainThread-1"];

    NSMutableDictionary *blockDictionary = [[CLActionManager sharedManager].blockDictionaryMapTable objectForKey:observer];
    if (!blockDictionary) {
        blockDictionary = [NSMutableDictionary dictionary];
    }
    [blockDictionary setObject:actionBlock forKey:actionBlockKey];

    NSMutableDictionary *mainThreadDictionary = [[CLActionManager sharedManager].mainThreadDictionaryMapTable objectForKey:observer];
    if (!mainThreadDictionary) {
        mainThreadDictionary = [NSMutableDictionary dictionary];
    }
    [mainThreadDictionary setObject:[NSNumber numberWithBool:mainThread] forKey:actionMainThreadKey];

    [[CLActionManager sharedManager].observerMapTable setObject:observer forKey:key];
    [[CLActionManager sharedManager].blockDictionaryMapTable setObject:blockDictionary forKey:observer];
    [[CLActionManager sharedManager].mainThreadDictionaryMapTable setObject:mainThreadDictionary forKey:observer];
    dispatch_semaphore_signal([CLActionManager sharedManager].semaphore);
}

  2.发送通知
根据观察者和监听类型动态生成Key,然后找出对应观察者的属性,然后一一调用。

+ (void)postType:(CLActionType)type dictionary:(NSDictionary *)dictionary {
    dispatch_semaphore_wait([CLActionManager sharedManager].semaphore, DISPATCH_TIME_FOREVER);
    NSArray<NSString *> *keyArray = [[[CLActionManager sharedManager].observerMapTable keyEnumerator] allObjects];
    NSString *identifier = [[self keyWithActionType:type] stringByAppendingString:@"-1"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH %@",identifier];
    NSArray<NSString *> *array = [keyArray filteredArrayUsingPredicate:predicate];
    for (NSString *key in array) {
        NSString *actionBlockKey = [key stringByAppendingString:@"-CLActionBlock-1"];
        NSString *actionMainThreadKey = [key stringByAppendingString:@"-CLActionMainThread-1"];
        id observer = [[CLActionManager sharedManager].observerMapTable objectForKey:key];
        NSMutableDictionary *blockDictionary = [[CLActionManager sharedManager].blockDictionaryMapTable objectForKey:observer];
        NSMutableDictionary *mainThreadDictionary = [[CLActionManager sharedManager].mainThreadDictionaryMapTable objectForKey:observer];
        void(^block)(id observer, NSDictionary *dictionary) = [blockDictionary objectForKey:actionBlockKey];
        BOOL mainThread = [[mainThreadDictionary objectForKey:actionMainThreadKey] boolValue];
        if (block) {
            if (mainThread) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    block(observer, dictionary);
                });
            }else {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    block(observer, dictionary);
                });
            }
        }
    }
    dispatch_semaphore_signal([CLActionManager sharedManager].semaphore);
}

  

3.使用协议的方式来分发事件
这里为了使用方便,采取了Block回调的方式来监听,但是我们也可以考虑协议分发的方式来实现,每个观察者遵守Action协议,发送通知的时候我们就可以找出对应观察者,调用协议中的方法来达到事件分发的目的。

总结
以上是对之前的代码进行改进,希望能够给大家帮助,demo地址—>>CLActionManager

作者:季末微夏
链接:https://www.jianshu.com/p/289f26db13c2

标签:sharedManager,dictionary,observer,CLActionManager,iOS,NSString,奇思妙想,key,block
来源: https://www.cnblogs.com/fadaijun/p/14117043.html