RACScheduler //RAC封装的多线程/单线程block调用接口。
特点:可以递归执行block/可以在后台线程环境下执行block/可以在指定的时间执行block/可以在指定时间开始,一定时间间隔重复执行block
RACScheduler // scheduler默认创建的RACTargetQueueScheduler。 RACScheduler的初始化没有队列,只是得到个名字。因此这个类似于抽象类。schedule的调用,依赖于继承类。或者是调用RACScheduler.currentScheduler类方法--RACScheduler.mainThreadScheduler主线程或者是当前线程NSThread.currentThread.threadDictionary[RACSchedulerCurrentSchedulerKey]
->RACTestScheduler//
->RACSubscriptionScheduler //subscriptionScheduler单例。实际包括一条RACTargetQueueScheduler类型的backgroundScheduler实例,由此来工作。
->RACQueueScheduler//
->RACTargetQueueScheduler //RACScheduler.mainThreadScheduler就是这个类型,只不过dispatch_set_target_queue是dispatch_get_main_queue()。
->RACImmediateScheduler//immediateScheduler单例
RACScheduler+Private.h
import "RACScheduler.h"
extern NSString * const RACSchedulerCurrentSchedulerKey;
扩展的作用。 私有
@interface RACScheduler ()
- (instancetype)subscriptionScheduler;
- (instancetype)initWithName:(nullable NSString )name;
@end
配合使用
spec.private_header_files = 'Headers/Private/.h'
@implementation RACQueueScheduler
pragma mark Lifecycle
-
(instancetype)initWithName:(NSString *)name queue:(dispatch_queue_t)queue {
NSCParameterAssert(queue != NULL);self = [super initWithName:name];
_queue = queue;
if !OS_OBJECT_USE_OBJC
dispatch_retain(_queue);
endif
return self;
}
@implementation RACTargetQueueScheduler
-
(instancetype)initWithName:(NSString *)name targetQueue:(dispatch_queue_t)targetQueue {
NSCParameterAssert(targetQueue != NULL);if (name == nil) {
name = [NSString stringWithFormat:@"org.reactivecocoa.ReactiveObjC.RACTargetQueueScheduler(%s)", dispatch_queue_get_label(targetQueue)];
}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];
}
//栈
//堆
//数据区、只读数据区
//代码段
//结论:
//1、数据区的作用域很大,block直接访问。
//2、栈变量。 __block。 改变指针。 指针指向的内容也改变。
//3、栈变量。 不改变指针,指针指向的内容改变。
//4、栈变量。 block里面改变指针。
//5、栈变量。加了__block的dispatch跟数据区的相同,block直接访问。
- (RACDisposable *)schedule:(void (^)(void))block {
NSCParameterAssert(block != NULL);
2018-01-17 17:57:33.987843+0800 ReactiveCocoaTest[25141:1238160] 777770x604000010fa0
2018-01-17 17:57:33.988021+0800 ReactiveCocoaTest[25141:1238160] 888880x604000010fa0
2018-01-17 17:57:33.988030+0800 ReactiveCocoaTest[25141:1238159] 999990x604000010fa0
RACDisposable *disposable = [[RACDisposable alloc] init];
NSLog(@"77777%p",disposable);
dispatch_async(self.queue, ^{
NSLog(@"99999%p",disposable);
if (disposable.disposed) return;
[self performAsCurrentScheduler:block];
});
NSLog(@"88888%p",disposable);
return disposable;
}
双目运算符可以省略冒号的值。
RACScheduler *scheduler = RACScheduler.currentScheduler ?: self.backgroundScheduler;
block懒加载
- (instancetype)init {
self = [super init]; - (instancetype)initWithDisposables:(NSArray *)otherDisposables {
self = [self init];
-
(RACDisposable *)after:(NSDate *)date repeatingEvery:(NSTimeInterval)interval withLeeway:(NSTimeInterval)leeway schedule:(void (^)(void))block {
NSCParameterAssert(date != nil);
NSCParameterAssert(interval > 0.0 && interval < INT64_MAX / NSEC_PER_SEC);
NSCParameterAssert(leeway >= 0.0 && leeway < INT64_MAX / NSEC_PER_SEC);
NSCParameterAssert(block != NULL);uint64_t intervalInNanoSecs = (uint64_t)(interval * NSEC_PER_SEC);
uint64_t leewayInNanoSecs = (uint64_t)(leeway * NSEC_PER_SEC);dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.queue);
dispatch_source_set_timer(timer, [self.class wallTimeWithDate:date], intervalInNanoSecs, leewayInNanoSecs);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);return [RACDisposable disposableWithBlock:^{
//只有加了这句11
dispatch_source_cancel(timer);
}];
}
//并且加了这句22。 定时器才能用。
self.dispose= [[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault name:@"111"] after:
[NSDate dateWithTimeIntervalSinceNow:5] repeatingEvery:5 withLeeway:1 schedule:^{
NSLog(@"22222");
}];
dispatch_time_t (^datesblock)(NSDate*d)=^(NSDate *date) {
double seconds = 0;
double frac = modf(date.timeIntervalSince1970, &seconds);
struct timespec walltime = {
.tv_sec = (time_t)fmin(fmax(seconds, LONG_MIN), LONG_MAX),
.tv_nsec = (long)fmin(fmax(frac * NSEC_PER_SEC, LONG_MIN), LONG_MAX)
};
dispatch_time_t tim= dispatch_walltime(&walltime, 0);
return tim;
};
dispatch_time_t tim3 = dispatch_time(DISPATCH_TIME_NOW, 5*NSEC_PER_SEC);
dispatch_time_t tim1 = dispatch_time_delay(5);
dispatch_time_t tim2 =datesblock([NSDate dateWithTimeIntervalSinceNow:5]);
dispatch_time_t的长整型值并没有必然联系,但是作用是等效的。
2018-01-18 16:29:33.964167+0800 ReactiveCocoaTest[11843:455353] 26853368747865===26853368747970===16930479894745437666
把可变参数转化为NSString
-
(instancetype)setNameWithFormat:(NSString *)format, ... {
if (getenv("RAC_DEBUG_SIGNAL_NAMES") == NULL) return self;NSCParameterAssert(format != nil);
va_list args;
va_start(args, format);NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);self.name = str;
return self;
}