07 - RAC与多线程
RAC
怎么实现多线程并发处理订阅?RAC
多线程模式怎么解决资源抢夺?RAC
怎么实现取消订阅的?
首选先我们 沿着
信息订阅
这条路往后走:
- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
NSCParameterAssert(subscriber != nil);
RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];
if (self.didSubscribe != NULL) {
// 创建调度者, 执行订阅
RACDisposable *schedulingDisposable = [RACScheduler.subscriptionScheduler schedule:^{
RACDisposable *innerDisposable = self.didSubscribe(subscriber);
[disposable addDisposable:innerDisposable];
}];
[disposable addDisposable:schedulingDisposable];
}
return disposable;
}
-
RACScheduler
Schedulers are used to control when and where work is performed
在RAC中真正执行订阅的block是由
RACScheduler
调度者去执行的.
RACScheduler
本质上就是对队列的封装, 就是一个Queue
且调度者由
RACScheduler
子类去实现, 其本身无法使用.
- 1.0 订阅信息的block
// 1.0 订阅信息的block
RACDisposable *schedulingDisposable = [RACScheduler.subscriptionScheduler schedule:^{
RACDisposable *innerDisposable = self.didSubscribe(subscriber);
[disposable addDisposable:innerDisposable];
}];
- 2.0 创建
subscriptionScheduler
// 其本身是一个单列, 在整个程序中存在一份; 毕竟调度者只要一个就好, 无需更多浪费资源
+ (instancetype) subscriptionScheduler {
static dispatch_once_t onceToken;
static RACScheduler *subscriptionScheduler;
dispatch_once(&onceToken, ^{
subscriptionScheduler = [[RACSubscriptionScheduler alloc] init];
});
return subscriptionScheduler;
}
-
3.0 执行
schedule
当期的
RACScheduler.currentScheduler
为nil时, 表示没有调度者, 则把block
交由backgroundScheduler
去调度当期的
RACScheduler.currentScheduler
不为nil时,表示当前存在调度者, 则立即执行block
- (RACDisposable *)schedule:(void (^)(void))block {
NSCParameterAssert(block != NULL);
if (RACScheduler.currentScheduler == nil) return [self.backgroundScheduler schedule:block];
block();
return nil;
}
// 获取`当前的Scheduler`
+ (instancetype)currentScheduler {
// 首先去线程字典中获取`当前的Scheduler`
RACScheduler *scheduler = NSThread.currentThread.threadDictionary[RACSchedulerCurrentSchedulerKey];
if (scheduler != nil) return scheduler;
// 当在主线程时 `当前的Scheduler`为 `mainThreadScheduler`
if ([self.class isOnMainThread]) return RACScheduler.mainThreadScheduler;
// 此时表示当前执行在子线程, 且没有`当前的Scheduler`
return nil;
}
// 获取 `_backgroundScheduler`
- (id)init {
self = [super initWithName:@"com.ReactiveCocoa.RACScheduler.subscriptionScheduler"];
if (self == nil) return nil;
_backgroundScheduler = [RACScheduler scheduler];
return self;
}
+ (instancetype)scheduler {
return [self schedulerWithPriority:RACSchedulerPriorityDefault];
}
+ (instancetype)schedulerWithPriority:(RACSchedulerPriority)priority {
return [self schedulerWithPriority:priority name:@"com.ReactiveCocoa.RACScheduler.backgroundScheduler"];
}
+ (instancetype)schedulerWithPriority:(RACSchedulerPriority)priority name:(NSString *)name {
return [[RACTargetQueueScheduler alloc] initWithName:name targetQueue:dispatch_get_global_queue(priority, 0)];
}
// RACTargetQueueScheduler实现
- (id)initWithName:(NSString *)name targetQueue:(dispatch_queue_t)targetQueue {
NSCParameterAssert(targetQueue != NULL);
if (name == nil) {
name = [NSString stringWithFormat:@"com.ReactiveCocoa.RACTargetQueueScheduler(%s)", dispatch_queue_get_label(targetQueue)];
}
// 创建一个串行队列, 且把当前队列的执行阶层设置为 `dispatch_get_global_queue`
dispatch_queue_t queue = dispatch_queue_create(name.UTF8String, DISPATCH_QUEUE_SERIAL);
if (queue == NULL) return nil;
dispatch_set_target_queue(queue, targetQueue);
return [super initWithName:name queue:queue];
}
-
subscriptionScheduler
执行schedule
- (RACDisposable *)schedule:(void (^)(void))block {
NSCParameterAssert(block != NULL);
RACDisposable *disposable = [[RACDisposable alloc] init];
// 把操作追加到 `self.queue 串行队列`
dispatch_async(self.queue, ^{
if (disposable.disposed) return;
[self performAsCurrentScheduler:block];
});
return disposable;
}
// 执行订阅的block
- (void)performAsCurrentScheduler:(void (^)(void))block {
NSCParameterAssert(block != NULL);
RACScheduler *previousScheduler = RACScheduler.currentScheduler;
NSThread.currentThread.threadDictionary[RACSchedulerCurrentSchedulerKey] = self;
@autoreleasepool {
block();
}
if (previousScheduler != nil) {
NSThread.currentThread.threadDictionary[RACSchedulerCurrentSchedulerKey] = previousScheduler;
} else {
[NSThread.currentThread.threadDictionary removeObjectForKey:RACSchedulerCurrentSchedulerKey];
}
}
-
RAC
多线程模式怎么解决资源抢夺?
1.0 模拟RAC多线程并发
for (NSInteger i = 0; i < 5; i++) {
dispatch_async(dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT), ^{
RACSignal *s1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@5];
return nil;
}];
[s1 subscribeNext:^(id x) {
}];
});
}
2.0 因为实在子线程处理ARC;
故 `RACScheduler.currentScheduler == nil`即当前schedule一直是nil, 则把block交给 bacggroundSchedule.
3.0 追加任务到队列, 此时由于是在子线程追加任务, 故线程各不相同
NSLog(@"追加任务到队列:%@ 追加线程:%@", self.queue, [NSThread currentThread]);
dispatch_async(self.queue, ^{
if (disposable.disposed) return;
[self performAsCurrentScheduler:block];
});
4.0 `bacggroundSchedule`是一个单列, 故追加任务的队列都是同一个queue (self.queue), 且该队列是串行队列
5.0 故开多线程出处理RAC, 其仅仅是把`订阅的block`追加到`同一个串行队列`这步操作在不同线程处理而已;
但真正执行订阅的block, 还是由串行队列+异步执行; 即每次只会开一条子线程去处理任务, 不会发生资源抢夺问题.
-
RAC
怎么实现取消订阅的?
RACDisposable *schedulingDisposable = [RACScheduler.subscriptionScheduler schedule:^{
// 订阅的block
}];
[disposable addDisposable:schedulingDisposable];
}
// 返回 schedulingDisposable
- (RACDisposable *)schedule:(void (^)(void))block {
NSCParameterAssert(block != NULL);
RACDisposable *disposable = [[RACDisposable alloc] init];
dispatch_async(self.queue, ^{
if (disposable.disposed) return;
[self performAsCurrentScheduler:block];
});
return disposable;
}
1.0 执行 `schedule:` 创建一个新的 RACDisposable 即`schedulingDisposable`, 并添加到 外部的`disposable`;
2.0 把订阅的block执行过程, 以异步的方式追加到同一个串行队列, 故只有当当前线程所有程序都执行完, 串行队列才会开启线程 `执行订阅的block`
3.0 当外面的disposable 释放后, 其里面的子disposable都会被释放, 当GCD回过头来执行订阅的block时, 发现自己的disposable被释放, 就立即结束不再执行block.