美文网首页
catch和catchTo

catch和catchTo

作者: 飞羽田海 | 来源:发表于2021-12-06 12:11 被阅读0次
    • catch

    Subscribes to the returned signal when an error occurs.
    当error发生时,订阅返回信号
    可以理解为捕获信号的error信息

       RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:@"我是发送的信号"];
            [subscriber sendError:[NSError errorWithDomain:@"www.huang"
                                                      code:0
                                                  userInfo:@{NSLocalizedDescriptionKey:@"错误信息"}]];
            return nil;
        }];
        RACSignal *newSignal = [signal catch:^RACSignal *(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
            // 注意如果这里使用 [RACSignal return:error] newSignal就不会走error的回调了
            return [RACSignal error:error];
        }];
        
        [newSignal subscribeNext:^(id x) {
            NSLog(@"%@",x);
        } error:^(NSError * error) {
            NSLog(@"=%@",error.localizedDescription);
        }];
     ***************
      打印日志:
      我是发送的信号
      错误信息
      =错误信息
    
    • catchTo

    Subscribes to the given signal when an error occurs.

    catchTocatch 相同的触发场景,都需要订阅者调用[subscriber sendError]方法时。

        RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:@"我是发送的信号"];
            // error occurs
            [subscriber sendError:[NSError errorWithDomain:@"www.huang"
                                                      code:0
                                                  userInfo:@{NSLocalizedDescriptionKey:@"错误信息"}]];
            return nil;
        }];
        
      //  RACSignal *newSignal = [signal catchTo:[RACSignal return:@(1122)]]; // 走next回调
        RACSignal *newSignal = [signal catchTo:[RACSignal error:[NSError errorWithDomain:@"www.huang" code:0 userInfo:@{NSLocalizedDescriptionKey:@"catch的错误信息"}]]]; // 走error回调
        [newSignal subscribeNext:^(id x) {
            NSLog(@"next:%@",x);
        } error:^(NSError *error) {
            NSLog(@"error:%@",error.localizedDescription);
        }];
    
     ***************
      打印日志:
      next:我是发送的信号
      error:catch的错误信息
    

    需要注意的是,当调用catchTo方法时,是可以拦截next回调或error回调的,取决于被catchTo的信号。

    • 源码
    - (RACSignal *)catch:(RACSignal * (^)(NSError *error))catchBlock {
        NSCParameterAssert(catchBlock != NULL);
    
        return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
            RACSerialDisposable *catchDisposable = [[RACSerialDisposable alloc] init];
    
            RACDisposable *subscriptionDisposable = [self subscribeNext:^(id x) {
                [subscriber sendNext:x]; //不会拦截原来的value; 原来的信号变为热信号,新信号被订阅会来到这里
            } error:^(NSError *error) {
                RACSignal *signal = catchBlock(error); // 外界传入的信号
                NSCAssert(signal != nil, @"Expected non-nil signal from catch block on %@", self);
                catchDisposable.disposable = [signal subscribe:subscriber];
            } completed:^{
                [subscriber sendCompleted];
            }];
    
            return [RACDisposable disposableWithBlock:^{
                [catchDisposable dispose];
                [subscriptionDisposable dispose];
            }];
        }] setNameWithFormat:@"[%@] -catch:", self.name];
    }
    
    - (RACSignal *)catchTo:(RACSignal *)signal {
        return [[self catch:^(NSError *error) {
            return signal; // 传入的信号当做新信号返回,所以订阅新信号的结果决定于传入的信号!
        }] setNameWithFormat:@"[%@] -catchTo: %@", self.name, signal];
    }
    

    相关文章

      网友评论

          本文标题:catch和catchTo

          本文链接:https://www.haomeiwen.com/subject/gmsgxrtx.html