GLPubSub 1.0.1

GLPubSub 1.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年4月

Allen Hsu 维护。



GLPubSub 1.0.1

中文说明(README)

GLPubSub 是 NSNotificationCenter 的包装器,旨在使 pub/sub 简单化。它是一个基于 NSObject 的分类,因此可以在任何 NSObject 的子类上调用。

安装

源文件

如果您不使用 CocoaPods 管理库,也可以直接导入源文件。

  • 下载 最新代码 并解压缩。
  • NSObject+GLPubSub.hNSObject+GLPubSub.m 导入到您的项目中。导入时请记得勾选“必要时复制项目”。

用法

由于 GLPubSub 基于局域通知中心,并在 [NSNotificationCenter defaultCenter] 上注册,因此也支持系统通知,例如:UIApplicationDidEnterBackgroundNotificationUIApplicationDidBecomeActiveNotificationUITextFieldTextDidChangeNotification 等。但是您将丢失通知的 userInfo

设置 PubSub 队列

GLPubSub 基于 NSNotificationCenter-addObserverForName:object:queue:usingBlock: 方法构建。您可以通过调用 NSObject+setPubSubQueue: 来设置此方法传给你的队列。

默认情况下 PubSub 队列为 nil,这意味着事件回调将在通知发布的队列上触发。您可以将队列显式设置为 [NSOperationQueue mainQueue] 以使所有回调在主线程上触发。

[NSObject setPubSubQueue:[NSOperationQueue mainQueue]];

使用选择器订阅事件

大多数情况下,我们使用 self 作为订阅者

[self subscribe:@"YourEventName" selector:@selector(yourEventHandler)];

您可以订阅某些特定对象发布的事件

[self subscribe:@"YourEventName" obj:somePublisher selector:@selector(yourEventHandler)];

如果只想触发处理程序一次,可以使用

[self subscribeOnce:@"YourEventName" selector:@selector(yourEventHandler)];

触发后,事件将自动取消订阅。

如果您的选择器定义接受一个参数,则可以传递代表事件的 GLEvent 对象到您的选择器。

@interface GLEvent : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) id obj;
@property (nonatomic, retain) id data;

其中,name 是事件名称,obj 是触发事件的发布者,data 是事件的其他数据。

使用处理程序块订阅事件

所有方法与上面类似,只是将选择器替换为处理程序块。

GLEventHandler 定义如下:

typedef void (^GLEventHandler)(GLEvent *event);

因此,您可以使用类似以下语法来订阅一些事件:

__weak __typeof__(self) weakSelf = self;
[self subscribe:UIApplicationDidEnterBackgroundNotification handler:^(GLEvent *event) {
    __strong __typeof__(weakSelf) strongSelf = weakSelf;
    [strongSelf appDidEnterBackground];
}];

在此处弱化自身对于避免保留循环很重要。对于带有选择器的方法,可以使用以下四种方法与处理程序块一起使用:

- (id)subscribe:(NSString *)eventName handler:(GLEventHandler)handler;
- (id)subscribe:(NSString *)eventName obj:(id)obj handler:(GLEventHandler)handler;
- (id)subscribeOnce:(NSString *)eventName handler:(GLEventHandler)handler;
- (id)subscribeOnce:(NSString *)eventName obj:(id)obj handler:(GLEventHandler)handler;

取消订阅

取消订阅一个事件

- (void)unsubscribe:(NSString *)eventName;

取消订阅所有已订阅的事件

- (void)unsubscribeAll;

虽然当实例释放时,观察者也会被释放,但仍建议手动取消订阅,例如在 -dealloc 方法中,或有时在 -viewDidDisappear 方法中,具体取决于您的需求。

- (void)dealloc
{
    [self unsubscribeAll];
}

发布事件

您可以简单地发布一个事件,不附加额外数据

[self publish:@"YourEventName"];

或附带额外数据,通常情况下是一个实践中的 NSDictionary 对象

[self publish:@"YourEventName" data:@{@"key": value}]

保留循环

由于所有观察者都通过 self 被保留,如果在您的 block 中保留了 self,则会出现保留循环。因此,您必须像上面提到的那样弱化自身。我们强烈建议使用 libextobjc 中的 EXTScope 进行弱化/强化,使用该库后,您的代码将类似于:

@weakify(self);
[self subscribe:UIApplicationDidEnterBackgroundNotification handler:^(GLEvent *event) {
    @strongify(self);
    [self appDidEnterBackground];
}];

许可证

GLPubSub 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。


GLPubSub (中文)

英文 README

GLPubSub 是 NSNotificationCenter 的封装,目标是简化 iOS 开发中的发布/订阅模式。因为它是一个 NSObject 类别的扩展,所以可以在任意 NSObject 的子类中使用。

安装

源文件

如果您的项目没有使用 CocoaPods 来管理第三方依赖,您也可以直接导入源文件。

  • 下载 最新代码 并解压
  • NSObject+GLPubSub.hNSObject+GLPubSub.m 导入到您的项目中,记得在导入时勾选 “如果需要,则复制项”。

使用

因为 GLPubSub 是基于 NSNotificationCenter 并注册在 [NSNotificationCenter defaultCenter] 的,所以 GLPubSub 也支持大部分系统通知,例如 UIApplicationDidEnterBackgroundNotificationUIApplicationDidBecomeActiveNotificationUITextFieldTextDidChangeNotification 等等,但在转发的过程中会丢弃系统通知的 userInfo 字段。

设置 PubSub 的队列

GLPubSub 主要基于 NSNotificationCenter-addObserverForName:object:queue:usingBlock: 方法。您可以通过调用 NSObject+setPubSubQueue: 方法来设置传入方法的 queue

默认传入的 queuenil,这意味着所有事件将在发布通知的线程中执行。您可以将它手动设置为 [NSOperationQueue mainQueue] 以使所有事件在主线程触发:

[NSObject setPubSubQueue:[NSOperationQueue mainQueue]];

通过 Selector 订阅事件

大多数情况下,我们使用 self 作为订阅者:

[self subscribe:@"YourEventName" selector:@selector(yourEventHandler)];

您也可以指定事件的发布者:

[self subscribe:@"YourEventName" obj:somePublisher selector:@selector(yourEventHandler)];

如果您希望您的方法只触发一次,您可以使用:

[self subscribeOnce:@"YourEventName" selector:@selector(yourEventHandler)];

这样当该事件被触发后,就会自动取消订阅。

您的方 může接受一个 GLEvent 参数,该参数包含有关被触发事件的相关信息。

@interface GLEvent : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) id obj;
@property (nonatomic, retain) id data;

name 是事件名,obj 是发布者,data 是附加信息。

通过 Block 订阅事件

方法和上述通过 selector 订阅的方法类似:

GLEventHandler 定义如下:

typedef void (^GLEventHandler)(GLEvent *event);

因此,您可以使用 block 来订阅一个事件:

__weak __typeof__(self) weakSelf = self;
[self subscribe:UIApplicationDidEnterBackgroundNotification handler:^(GLEvent *event) {
    __strong __typeof__(weakSelf) strongSelf = weakSelf;
    [strongSelf appDidEnterBackground];
}];

这里进行弱化是为了避免循环引用。对于前面 selector 的方法,使用 block 也有 4 种调用方法:

- (id)subscribe:(NSString *)eventName handler:(GLEventHandler)handler;
- (id)subscribe:(NSString *)eventName obj:(id)obj handler:(GLEventHandler)handler;
- (id)subscribeOnce:(NSString *)eventName handler:(GLEventHandler)handler;
- (id)subscribeOnce:(NSString *)eventName obj:(id)obj handler:(GLEventHandler)handler;

取消订阅

取消订阅某个事件:

- (void)unsubscribe:(NSString *)eventName;

取消订阅所有事件:

- (void)unsubscribeAll;

尽管当实例被销毁时,存在于 associated object 中的观察者也会被销毁,但仍然建议手动取消订阅,如在 -dealloc-viewDidDisappear 方法中取消订阅,具体取决于您的需求。

- (void)dealloc
{
    [self unsubscribeAll];
}

发布事件

您可以直接发布事件:

[self publish:@"YourEventName"];

也可以附加一些数据,通常情况下我们会传递一个 NSDictionary 以附加更多结构化数据:

[self publish:@"YourEventName" data:@{@"key": value}]

循环引用

因为所有生成的观察者都会被 self 引用,所以当你的 block 引用 self 的时候就会形成循环引用导致实例无法被释放,所以你必须弱引用 self。强烈推荐使用 libextobjc 中的 EXTScope 来进行弱化/强化:

@weakify(self);
[self subscribe:UIApplicationDidEnterBackgroundNotification handler:^(GLEvent *event) {
    @strongify(self);
    [self appDidEnterBackground];
}];

许可证

GLPubSub 基于 MIT 协议开源,详情见 LICENSE 文件。