快捷搜索:

用于取消或者清理订阅者的资源

简介

ReactiveCocoa是叁个依据函数响应式编制程序思想(Funcation Reactive Programming,简单称谓FRP)的框架。由多少个关键的一对构成,如下:

非随机信号:比如RACSignal,他得以被订阅,订阅后开展逻辑管理恐怕数额传递。

订阅者:举个例子RACSubscriber,表示订阅者的情致。用于订阅和发送数据。它是二个磋商,由具体的类达成。

清理者:举例RACDisposable,用于撤销也许清理订阅者的财富。

RACSubject:能够算作三个实信号,也足以充任时域信号发送者。

八个回顾的流水生产线分析

最宗旨的流程能够是,创制四个非随机信号,然后创设叁个订阅者并且订阅这么些时限信号。

//首先创建一个信号RACSignal *signal = [RACSignal create:^(id<RACSubscriber> subscriber) { [subscriber sendNext:@"test"]; //被subscriber订阅后,触发subscriber的sendNext方法,传递数据}]; [signal subscribeNext:^ { NSLog; //将传递的数据数据输出}];//输出"test"//创建一个RACDynamicSignal信号,指定了被订阅后的回调逻辑  (RACSignal *)create:(id<RACSubscriber>))didSubscribe { RACDynamicSignal *signal = [[self alloc] init]; signal->_didSubscribe = [didSubscribe copy]; return [signal setNameWithFormat:@" create:"];}//创界一个订阅者,并且订阅信号- (RACDisposable *)subscribeNext:nextBlock error:(NSError *error))errorBlock completed:completedBlock { RACLiveSubscriber *subscriber = [RACLiveSubscriber subscriberWithNext:nextBlock error:errorBlock completed:completedBlock]; subscriber.signal = self; //subscriber关联信号 [self attachSubscriber:subscriber]; //信号被subscriber订阅 return subscriber.disposable;}

1.create:方法创造一个RACDynamicSignal类型的非确定性信号signal,並且钦点了被订阅后的回调逻辑。

2.创制叁个RACLiveSubscriber类型的订阅者subscriber,subscriber分别维护了3个block,用于拍卖next事件,error事件和completed事件的多少,本例的error和completed回调均为nil。

3.subscriber订阅signal,RACDynamicSignal类型的时域信号在attachSubscriber方法中会触发_didSubscribe回调,并传到subscriber。在回调逻辑里,subscriber触发next事件,传递数据。

4.subscriber的next回调采用到多少后,输出。

订阅方法

RACSignal时限信号类提供了一多级订阅时域信号的措施,但都是基于上文的subscribeNext:error:completed:,例如:

- (RACDisposable *)subscribeNext:nextBlock { return [self subscribeNext:nextBlock error:nil completed:nil];}- (RACDisposable *)subscribeNext:nextBlock completed:completedBlock { return [self subscribeNext:nextBlock error:nil completed:completedBlock];}- (RACDisposable *)subscribeError:(NSError *error))errorBlock { return [self subscribeNext:nil error:errorBlock completed:nil];}- (RACDisposable *)subscribeCompleted:completedBlock { return [self subscribeNext:nil error:nil completed:completedBlock];}

还应该有三个比较区别的法子是:

- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber { RACLiveSubscriber *liveSubscriber; if (subscriber == nil) { //创建一个RACLiveSubscriber,维护三个block,即next、error和completed,但只都是nil liveSubscriber = [RACLiveSubscriber subscriberWithNext:nil error:nil completed:nil]; } else { //创建一个RACLiveSubscriber,维护三个block,即next、error和completed liveSubscriber = [RACLiveSubscriber subscriberForwardingToSubscriber:subscriber]; } liveSubscriber.signal = self; [self attachSubscriber:liveSubscriber]; return liveSubscriber.disposable;}

该方法依照二个已部分subscriber,套三个新的RACLiveSubscriber类型的liveSubscriber在外面,liveSubscriber内部的next、error和completed回调会接触原subscriber的照顾回调。

  (instancetype)subscriberForwardingToSubscriber:(id<RACSubscriber>)subscriber { NSCParameterAssert(subscriber != nil); //新liveSubscriber的三个block分别去调用原subscriber的三个block RACLiveSubscriber *liveSubscriber = [self subscriberWithNext:^ { [subscriber sendNext:x]; } error:^(NSError *error) { [subscriber sendError:error]; } completed:^{ [subscriber sendCompleted]; }]; [subscriber.disposable addDisposable:liveSubscriber.disposable]; [liveSubscriber.disposable addDisposable:[RACDisposable disposableWithBlock:^{ [subscriber.disposable removeDisposable:liveSubscriber.disposable]; }]]; return liveSubscriber;}

将新liveSubscriber的disposable参与原subscriber的disposable队列中,关联两个的disposable对象。

RACDisposable

RACDisposable相关类担当清理订阅者的财富,举例创造三个RACDisposable的点子:

RACDisposable *disposable = [RACDisposable disposableWithBlock:^{ //...执行相关逻辑}];

disposable对象里面维护一个block,当实践dispose方法时,会进行block。RACCompoundDisposable对象是RACDisposable对象的子类,功效是保卫安全贰个体系,里面贮存了若干个disposable对象,RACCompoundDisposable对象进行dispose方法时,将队列中的disposable对象依次推行dispose方法。

和订阅者结合使用情形下,每种订阅者会爱慕七个RACCompoundDisposable对象disposable,如下:

@protocol RACSubscriber <NSObject>@required@property (nonatomic, strong, readonly) RACCompoundDisposable *disposable;- sendNext:value;- sendError:(NSError *)error;- sendCompleted;@end

除外达成多个事件触发的方法,还可能有一个RACCompoundDisposable对象disposable,以RACLiveSubscriber为例,在早先化的时候会成立二个RACCompoundDisposable对象,如下:

- init { ... //创建一个selfDisposable对象,用于将三个事件block置为nil RACDisposable *selfDisposable = [RACDisposable disposableWithBlock:^{ @strongify; if (self == nil) return; OSSpinLockLock(&self->_spinLock); self.next = nil; self.error = nil; self.completed = nil; OSSpinLockUnlock(&self->_spinLock); }]; //将selfDisposable对象加入队列对象中 _disposable = [RACCompoundDisposable compoundDisposableWithDisposables:@[ selfDisposable ]]; return self;}

随着深入分析上面的subscriberForwardingToSubscriber:方法

[subscriber.disposable addDisposable:liveSubscriber.disposable];[liveSubscriber.disposable addDisposable:[RACDisposable disposableWithBlock:^{ [subscriber.disposable removeDisposable:liveSubscriber.disposable];}]];

将新的liveSubscriber对象的disposable对象参与原subscriber对象的disposable队列中。之所以这么做,小编的领会是liveSubscriber基于subscriber创制,由此当subscriber在某种景况dispose,相关联的liveSubscriber失去存在乎义,也要dispose,将其几个事件的block置为nil,同不经常候将liveSubscriber的disposable对象从队列中移除。

attachSubscriber:

attachSubscriber:担负signal被订阅后的逻辑,基类RACSignal暗中同意不达成任何效果,如下:

- attachSubscriber:(RACLiveSubscriber *)subscriber { NSCAssert(NO, @"This method must be overridden by subclasses.");}

差别的signal子类达成逻辑不均等,以create:方法创制的RACDynamicSignal类为例,如下:

- attachSubscriber:(RACLiveSubscriber *)subscriber { NSCParameterAssert(subscriber != nil); if (self.didSubscribe != NULL) { //执行signal的_didSubscribe回调 self.didSubscribe(subscriber); }}

以RACReturnSignal类为例,如下:

- attachSubscriber:(RACLiveSubscriber *)subscriber { NSCParameterAssert(subscriber != nil); [subscriber sendNext:self.value]; //触发subscriber的next事件,将value作为数据传递 [subscriber sendCompleted]; //触发subscriber的comple事件}
总结

RAC框架通过block回调的不二诀窍完成FRP编制程序思想,每一回操作会触发二个应和的block作为响应,传递数据,在写法上使代码高聚焦,方便处理。

本文由澳门新葡萄京8455官网发布于澳门新葡萄京8455官网,转载请注明出处:用于取消或者清理订阅者的资源

您可能还会对下面的文章感兴趣: