1.RACSubject
id
//先订阅 后发送信号源
RACSubject * subject=[RACSubject subject];
[subject subscribeNext:^(id x) { //创建第一个订阅者
NSLog(@"x1:%@",x);
NSLog(@"");
}];
[subject subscribeNext:^(id x) {//创建第二个订阅者 保存subscribers数组
NSLog(@"x2:%@",x);
NSLog(@"");
}];
[subject sendNext:@1]; //遍历订阅者subscribers 同时发送消息
延迟
RACDelaySubject
//先发送 后订阅
RACReplaySubject * subject=[RACReplaySubject subject];
[subject sendNext:@1]; //发送消息 for循环遍历 这个时候发现没有订阅者 根本无法调用block
[subject subscribeNext:^(id x) { //创建内部订阅者 RACReplaySubject 实现父类方法 并且 遍历valuesReceived 调用sendNextBlock
NSLog(@"x1:%@",x);
NSLog(@"");
}];
2.Rac集合rac_sequence
开启异步线程遍历。
NSArray *data=@[@1,@2,@3,@4,@5,@6,@7,@8];
[data.rac_sequence.signal subscribeNext:^(id x) {
NSLog(@"%@",x);
NSLog(@"开启一个新的线程");
} completed:^{
NSLog(@"刷新ui");
}];
NSDictionary *dic = @{@"name":@"xiaohua",@"xingbie":@"woman"};
[dic.rac_sequence.signal subscribeNext:^(RACTuple * x)
{
NSString *key=x[0];
NSString *value=x[1];
RACTupleUnpack(NSString *key2,NSString *value2) = x;//元组
NSLog(@"key1:%@,value1%@;key2:%@,value:2%@",key,value,key2,value2);
}];
RACTuple * x =RACTuplePack(@1,@2,@3);
```
3.RACMulticastConnection组播
RACSignal * signal= [RACSignal createSignal:^RACDisposable * _Nullable(id
NSLog(@"Send Request");
[subscriber sendNext:@1];
return nil;
}];
/*执行1次Send Request*/
RACMulticastConnection *conn=[signal publish];
[conn.signal subscribeNext:^(id x) {
NSLog(@"conn1:%@",x);
NSLog(@"");
}];
[conn.signal subscribeNext:^(id x) {//创建第二个订阅者 保存subscribers数组
NSLog(@"conn2:%@",x);
NSLog(@"");
}];
[conn connect];
/*执行两次Send Request*/
[signal subscribeNext:^(id x) { //创建第一个订阅者
NSLog(@"x1:%@",x);
NSLog(@"");
}];
[signal subscribeNext:^(id x) {//创建第二个订阅者 保存subscribers数组
NSLog(@"x2:%@",x);
NSLog(@"");
}];
4.RacCommond
4.1
-(void)doCommond
{
//监听按钮 网络请求
RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
{
//command block
NSLog(@"开始RACCommand_block:%@",input);
NSLog(@"");
//必须返回一个信号 [RACSignal empty]; 返回nil 会崩溃
return [RACSignal createSignal:^RACDisposable *(id
//信号block 内部被订阅
NSLog(@"开始RACSignal_block");
[subscriber sendNext:@"RACSignalSendNext"];
[subscriber sendCompleted];//判断是否完成会用到的
return nil;
}];
}];
//订阅到内部信号
//方式1:信号中的信号 一开始获取不到内部信号
[command.executionSignals subscribeNext:^(id x) {
NSLog(@"subcrible:%@",x);
NSLog(@"");
[x subscribeNext:^(id x) {
NSLog(@"next subcrible:%@",x);
NSLog(@"");
}];
}];
//方式2:execute获取内部信号
[[command execute:@"1"] subscribeNext:^(id x) {
NSLog(@"subcrible:%@",x);
NSLog(@"");
}];
//方式3:switchToLatest获取最近发送的信号
[command.executionSignals.switchToLatest subscribeNext:^(id x) {
NSLog(@"executionSignals.switchToLatest:%@",x);
NSLog(@"");
}];
//监听命令是否完成 第一次不准确
[command.executing subscribeNext:^(id x) {
BOOL isExecuting = [x boolValue];
if(isExecuting)
{
NSLog(@"正在执行");
}else
{
NSLog(@"完成执行");
}
}];
//监听命令是否完成 上面第一次不准 需要跳过第一次
[[command.executing skip:1] subscribeNext:^(id x) {
BOOL isExecuting = [x boolValue];
if(isExecuting)
{
NSLog(@"正在执行");
}else
{
NSLog(@"完成执行");//需要信号发送sendCompleted
}
}];
[command execute:@"commond"];
}
5.doSingalForSelector
-(void)doSingalForSelector
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(200);
make.width.height.mas_equalTo(100);
}];
btn.backgroundColor = [UIColor greenColor];
//按钮点击事件
[[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"btn %@",x);
NSLog(@"");
self.count++;
}];
}
6.KVO
-(void)doSingalForKVO
{
//@"count" == keypath(self,count)
//方式1
[[self rac_valuesAndChangesForKeyPath:@keypath(self,count) options:NSKeyValueObservingOptionNew observer:self] subscribeNext:^(id x) {
RACTupleUnpack(NSNumber *value,NSDictionary *dic) =x;
NSLog(@"value:%@,dic:%@",value,dic);
NSLog(@"kvo:%@",x);
}];
//方式2
[RACObserve(self, count) subscribeNext:^(id x)
{
NSLog(@"kvo:%@",x);
}];
// [self removeObserver:self forKeyPath:NSStringFromSelector(@selector(count))];
}
7.doSingalForNotification
-(void)doSingalForNotification
{
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notify" object:nil] subscribeNext:^(id x) {
NSLog(@"x::%@",x);
}];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:nil];
}
8.监听
-(void)doMonitor
{
UITextField *tf = [[UITextField alloc] init];
UILabel *label = [[UILabel alloc] init];
[self.view addSubview:tf];
[self.view addSubview:label];
[tf mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(200);
make.width.height.mas_equalTo(100);
}];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(200);
make.left.mas_equalTo(320);
make.width.height.mas_equalTo(100);
}];
tf.backgroundColor = [UIColor greenColor];
方式1:信号订阅
[tf.rac_textSignal subscribeNext:^(id x) {
NSLog(@"x:%@",x);
label.text =x;
}];
//方式2:事件绑定机制
RAC(label,text)=tf.rac_textSignal;
}
9.doLiftSignal
//处理一个界面 多个请求
-(void)doLiftSignal
{
RACSignal *homeSignal=[ RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
NSLog(@"loadHome");
[subscriber sendNext:@"loadHome"];
});
return nil;
}];
RACSignal *bannerSignal=[ RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
NSLog(@"loadBanner");
[subscriber sendNext:@"loadBanner"];
});
return nil;
}];
[self rac_liftSelector:@selector(updateUI:banner:) withSignalsFromArray:@[homeSignal,bannerSignal]];
}
-(void)updateUI:(RACSignal *)homeSignal banner:(RACSignal *)bannerSignal
{
NSLog(@"updateUI");
}
10.doMap
/*
map方法,根据原信号创建了一个新的信号,并且变换了信号的输出值。这两个信号具有明显的先后顺序关系。而flattenMap方法,直接生成了一个新的信号,这两个信号并没有先后顺序关系,属于同层次的平行关系
*/
-(void)doSignalMap
{
UITextField *tf = [[UITextField alloc] init];
[self.view addSubview:tf];
[tf mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(200);
make.width.mas_equalTo(140);
make.height.mas_equalTo(40);
}];
tf.backgroundColor = [UIColor greenColor];
// //方式1:flattenMap信号中的信号
// [[tf.rac_textSignal flattenMap:^RACStream *(id value) {
//
// NSLog(@"flattenMap:%@",value);
// NSString *result= [NSString stringWithFormat:@"result_%@",value];
// return [RACReturnSignal return:result];
//
// }] subscribeNext:^(id x) {
//
// NSLog(@"flattenMap--:%@",x);
// }];
//方式2:map 值
[[tf.rac_textSignal map:^id(id value) {
NSString *result= [NSString stringWithFormat:@"result_%@",value];
return result;
}] subscribeNext:^(id x) {
NSLog(@"map--:%@",x);
}];
}
11.