RACScheduler  信号调度器,是一个线性执行队列,rac中的信号可以在RACScheduler上执行任务、发送结果,底层用GCD封装的。

rac中提供生成线程的几个方法:

1:scheduler,这是一个异步线程,不会对主线程造成堵塞,异步执行

[[RACScheduler scheduler] schedule:^{
NSLog(@"当前线程:%@",[RACScheduler currentScheduler]);
}];

2:immediateScheduler ,立即执行的线程,其实就是在主线程执行的

[[RACScheduler immediateScheduler] schedule:^{
NSLog(@"当前线程:%@",[RACScheduler currentScheduler]);
}];

  输出日志: org.reactivecocoa.ReactiveObjC.RACScheduler.mainThreadScheduler

3:mainThreadScheduler   获取主线程调度器。

[[RACScheduler mainThreadScheduler] schedule:^{
NSLog(@"当前线程:%@",[RACScheduler currentScheduler]);
}];

这个其实和immediateScheduler差不多的玩意

4:currentScheduler   前几个中就能看到,就是获取当前线程调度器。

[RACScheduler currentScheduler]

5:如何指定调度器的优先级。具体使用方法schedulerWithPriority

rac提供了枚举类型:

RACSchedulerPriorityLow

RACSchedulerPriorityDefault

RACSchedulerPriorityBackground

RACSchedulerPriorityHigh

具体使用方法如下:

//  思考。 如何指定某个线程的优先级
[[RACScheduler schedulerWithPriority:RACSchedulerPriorityLow] schedule:^{
NSLog(@"aaaaa:%@",[RACScheduler currentScheduler]);
}]; [[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault] schedule:^{
NSLog(@"bbbbb:%@",[RACScheduler currentScheduler]);
}]; [[RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground] schedule:^{
NSLog(@"cccccc:%@",[RACScheduler currentScheduler]);
}]; [[RACScheduler schedulerWithPriority:RACSchedulerPriorityHigh] schedule:^{
NSLog(@"dddddd:%@",[RACScheduler currentScheduler]);
}];

------------------    以下列举一下常用的用于调度、执行任务的方法

1:  schedule   立刻执行

上面有使用案例

2:afterDelay    会将开启的线程休眠到指定时间后执行block

// 异步线程
[[RACScheduler mainThreadScheduler] afterDelay: schedule:^{
NSLog(@"--------%@",[RACScheduler currentScheduler]);
}];

实际使用可以用来做延迟的操作。。。。。

3:以下是参考别的文章拿来的解释

-(RACDisposable *)after:(NSDate *)date repeatingEvery:(NSTimeInterval)intervalwithLeeway:(NSTimeInterval)leeway schedule:(void(^)(void))block;
对这个方法的解释:

当前线程休眠date时间之后执行,然后每隔interval时间重复执行,leeway这个参数是为dispatch source指定一个期望的定时器事件精度,让系统能够灵活地管理并唤醒内核。例如系统可以使用leeway值来提前或延迟触发定时器,使其更好地与其它系统事件结合。创建自己的定时器时,应该尽量指定一个leeway值。不过就算指定leeway值为0,也不能完完全全期望定时器能够按照精确的纳秒来触发事件
 
4:deliverOn   线程的切换  在设置的调度中发送信号值,但操作封包依然在原来的调度里进行
 
NSArray *temArr = @[@"",@""];
[[temArr.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) { }];

大概意思就是把这个遍历的操作放到主线程中执行了,否则遍历的操作是在异步线程中执行的

5:subscribeOn    在设置的调度中发送信号和执行都在同一个Scheduler操作

案例

[[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
NSLog(@"sendSignal%@",[NSThread currentThread]);
[subscriber sendNext:@"我是发送的数据"];
[subscriber sendCompleted];
return nil;
}] subscribeOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) {
NSLog(@"xxxx:%@",x);
NSLog(@"receiveSignal%@",[NSThread currentThread]);
}];

说明一下subscribeOn和deliverOn的区别。      其实说白了,deliverOn 会让发送信号和接收信号不在一个线程中。就想上面的遍历数组的例子,目的就是不想让接受在异步中,不然得处理代码执行顺序的问题。     而subscribeOn刚好相反,会让发送信号和接收信号在一个线程中。

//   注意数组的遍历发送信号一定是在异步线程中执行的,,所以用subscribeOn,然后设置在主线程中接受是无效的。

代码如下:

NSArray *temArr = @[@"",@""];
[[temArr.rac_sequence.signal subscribeOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",[RACScheduler currentScheduler]);
}];

这个设置[RACScheduler mainThreadScheduler]  是无效的。。。。

6:timeout   超时。。。

详细事例:

static int time = ;
[[RACSignal interval: onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
time ++;
NSLog(@"%d",time);
}]; [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
return nil;
}] timeout: onScheduler:[RACScheduler currentScheduler]] subscribeNext:^(id _Nullable x) {
NSLog(@"xxxxx:%@",x);
} error:^(NSError * _Nullable error) {
NSLog(@"error:%@",error);
}];

7:interval   这玩意就是定时器。

事例见上个案例。

---------------

ReactiveCocoa操作须知:

所有的信号(RACSignal)都可以进行操作处理,因为所有操作方法都定义在RACStream.h中,因此只要继承RACStream就有了操作处理方法

ReactiveCocoa操作思想:

运用的是Hook(钩子)思想,Hook是一种用于改变API(应用程序编程接口:方法)执行结果的技术.

•Hook用处:截获API调用的技术。

Hook原理:在每次调用一个API返回结果之前,先执行你自己的方法,改变结果的输出

 
 
----------
就到这吧    over

rac 关于RACScheduler的一点学习的更多相关文章

  1. 关于Set和Map数据结构的一点学习

    关于js的Set和Map结构的学习和记录 对阮一峰老师的ES6入门和网上有关资料的的一点学习和记录 1.Set数据结构 Set构造函数的参数是一个可遍历( iterator)对象 Set中的成员值是唯 ...

  2. Vagrant 安装Oracle19c RAC测试环境的简单学习

    1. 学习自网站: https://xiaoyu.blog.csdn.net/article/details/103135158 简单学习了下 能够将oracle RAC开起来了 但是 对后期的维护和 ...

  3. FloatingActionButton的一点学习感悟

    最近在学习android材料设计的新控件,前面一篇文章讲到 CoordinatorLayout 结合几个新控件可以实现的几个效果.其中第一个是,Coordinatorlayout + Floating ...

  4. 嵌入式Linux的一点学习心得

    Linux本身是一个发展中的操作系统.它有很多前期不完善的机制,被后代用新的机制代替.但是老的机制不可能一下子就消亡,因此由于“历史原因”,会产生很多新旧机制共存的情况.而且Linux的教科书数不胜数 ...

  5. 关于 truncate table 的一点学习札记

    ---下面整理笔记来之 itpub 的各位前辈的语录.这里做了一个汇总.仅供学习. truncate table后,oracle会回收表和其表中所在的索引到initial 大小,也就是初始分配的seg ...

  6. numpy的一点学习

    1.Numpy模块 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体验在原生 ...

  7. javascript的一点学习

    最近用vue.js用的很爽,在全栈开发的路上一路狂奔,发现后台跟前台一起确实更有意义. 记录一个比较有意思的bug: 目标是对一个全局的paramList进行json格式化显示.代码借鉴了 http: ...

  8. AngularJS的一点学习笔记

    ng-options="item.action for item in todos" ng-options表达式的基本形式, 形如 "<标签> for < ...

  9. fork 函数的一点学习

    昨天某位少年问了我一个问题,#include<stdio.h> int main() { fork(); fork(); fork(); printf("hello " ...

随机推荐

  1. leetcode907 Sum of Subarray Minimums

    思路: 对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果.时间复杂度O(n). 实现: class Solution { public: int sumSubarr ...

  2. 在HTML5 中使用 kindeditor 的方法

    1.打开:http://kindeditor.net/ke4/examples/default.html 2.查看源代码,另存为 3.打开http://kindeditor.net/demo.php, ...

  3. Java向上保留两位小数

    setScale(2, BigDecimal.ROUND_UP) 例如:0.035 运算结果 为0.01

  4. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  5. chrome 监听touch类事件报错:无法被动侦听事件preventDefault

    先上错误信息: Unable to preventDefault inside passive event listener due to target being treated as passiv ...

  6. LC 599. Minimum Index Sum of Two Lists

    题目描述 Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fav ...

  7. php源码安装执行configure报错error: off_t undefined; check your library configuration

    php安装执行configure报错error: off_t undefined; check your library configuration vim /etc/ld.so.conf 添加如下几 ...

  8. postman的安装与使用方法介绍

    软件介绍 在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具,现在流行的这种工具也挺多的,像火狐 ...

  9. RPA自定义脚本打开文件夹

    import os import subprocess from rpa.web.common.utils import convert_2_unicode def startfile(filenam ...

  10. LeetCode 328——奇偶链表(JAVA)

    给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1),时 ...