阅读 1171

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

前言

在之前的文章iOS奇思妙想之使用block替代通知(一) )中,自己实现了通知功能。在之前的实现中,使用了Runtime进行动态绑定,虽然达到了最后的效果,但是也会增加耦合。 #解决耦合 既然动态绑定属性会增加耦合,那么我们可以考虑不让观察者动态绑定属性来实现。这里参考之前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