基本信息

父类 NSObject
子类 RACImmediateScheduler
RACQueueScheduler
RACSubscriptionScheduler
RACTestScheduler
类含义 RAC调度类
遵循的协议
属性

例子

显示网络下载的图片

    RAC(self.imageView, image) = [[RACSignal startEagerlyWithScheduler:[RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground]
                                                                 block:^(id <RACSubscriber> subscriber) {
        NSError *error;
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://ww3.sinaimg.cn/bmiddle/7128be06jw1ei4hfthoj3j20hs0bomyd.jpg"]
                                             options:NSDataReadingMappedAlways
                                               error:&error];
        if(error) {
            [subscriber sendError:error];
        }
        else {
            [subscriber sendNext:[UIImage imageWithData:data]];
            [subscriber sendCompleted];
        }
    }] deliverOn:[RACScheduler mainThreadScheduler]];

这段代码会在后台线程立即发起一个请求,然后传递到主线程上更新UI

  • 发起请求: + (RACSignal *)startEagerlyWithScheduler:(RACScheduler *)scheduler block:(void (^)(id<RACSubscriber> subscriber))block;
  • 信号传递: - (RACSignal *)deliverOn:(RACScheduler *)scheduler

方法介绍

    + (RACScheduler *)mainThreadScheduler;

主线程调度


    + (RACScheduler *)currentScheduler;

当前队列调度


    + (RACScheduler *)scheduler;

这方法相当于执行了[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault];


    + (RACScheduler *)schedulerWithPriority:(RACSchedulerPriority)priority;

指定等级的异步并发队列,这个其实调用的是

[[RACTargetQueueScheduler alloc] initWithName:name targetQueue:dispatch_get_global_queue(priority, 0)];

其中RACTargetQueueScheduler类的父类是RACQueueScheduler,而且RACQueueScheduler中有个方法是:

- (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;
}

可以看到最终用的还是dispatch_async的异步调用


bawn
515 声望44 粉丝

欢迎技术交流