项目中实际需要是三种操作依次执行,顺序相互依赖。如果没有依赖的处理方式很多,但相互依赖时需要对信号进行 FlattenMap处理。
1. 依次创建信号
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"打蛋液");
[subscriber sendNext:@"蛋液"];
[subscriber sendCompleted];
return nil;
}] ;
RACSignal *signal2 = [[signal1 flattenMap:^__kindof RACSignal * _Nullable(id _Nullable value) {
NSLog(@"把%@倒进锅里面煎",value);
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//[subscriber sendNext:@"煎蛋"];
NSError *error = [NSError errorWithDomain:@"访问权限" code:1000 userInfo:nil];
[subscriber sendError:error];
[subscriber sendCompleted];
return nil;
}];
}] ;
RACSignal *signal3 = [signal2 flattenMap:^__kindof RACSignal * _Nullable(id _Nullable value) {
NSLog(@"把%@装载盘里",value);
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"上菜"];
[subscriber sendCompleted];
return nil;
}];
}] ;
2. 观察最终结果
[signal3 subscribeNext:^(id _Nullable x) {
NSLog(@"====%@",x);
} error:^(NSError * _Nullable error) {
# 任何一个步骤错误都会进入这个Block
NSLog(@"%@",error);
}];