美文网首页
手写一个iOS事件总线EventBus

手写一个iOS事件总线EventBus

作者: babyloveblues | 来源:发表于2021-07-27 15:01 被阅读0次

    1、什么是事件总线

    事件总线是一个基于NSNotification的发布/订阅框架。

    2、实现目标


    (1)订阅某个Notification事件,监控通知的发送;
    (2)当监听对象从内存中销毁(dealloc)的时候,自动移除监听

    3、使用

    现已支持cocoapods

    pod 'ViaBus'
    

    (1)发送广播到总线

    [VIABUS publishNotification:@"didlogin" broadcastContent:userInfo];
    

    (2)订阅广播

    [VIABUS subscribeEventWithEventname:@"didlogin" andTaget:self handler:^(NSString * eventName, id object) {
      NSLog(@"收到通知");
    }];
    

    (3)手动取消广播

    [VIABUS unsubscribeEventWithEventName:@"didlogin" target:self];
    

    (4)自动释放

    当监听人实例被释放的时候,会自动移除订阅。新的广播发送的时候,不会再被监听。

    4、源代码&Demo

    https://github.com/BBC6BAE9/ViaBus

    相关文章

      网友评论

          本文标题:手写一个iOS事件总线EventBus

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