ReactiveCocoa--RACScheduler

基本信息

父類 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;

主線程調度app


+ (RACScheduler *)currentScheduler;

當前隊列調度異步


+ (RACScheduler *)scheduler;

這方法至關於執行了[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault];async


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

指定等級的異步併發隊列,這個其實調用的是spa

[[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的異步調用code

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息