美文网首页
RAC的Error操作

RAC的Error操作

作者: 凯司机 | 来源:发表于2021-05-12 22:28 被阅读0次

    将某个值变成一个错误的信号:

    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      [subscriber sendNext:@1];
      [subscriber sendNext:@2];
      [subscriber sendNext:@3];
      [subscriber sendNext:@4];
      [subscriber sendNext:@5];
      [subscriber sendCompleted];
      return [RACDisposable disposableWithBlock:^{
          NSLog(@"signalA1完成");
      }];
    }];
    RACSignal *signalB = [[signalA map:^id(NSNumber *value) {
      if (value.integerValue == 5) {
          return [RACSignal error:[NSError errorWithDomain:@"0" code:0 userInfo:nil]];
      } else {
          return [RACSignal return:value];
      }
    }] flatten];
    
    [[signalB subscribeNext:^(RACSignal *x) {
      NSLog(@"subscribeNext:%@",x);
    }] dispose];
    
    [signalB subscribeError:^(NSError *error) {
      NSLog(@"subscribeNext:%@",error.description);
    }];
    

    2021-04-15 16:43:49.033757+0800 NetSchool[3863:1105334] subscribeNext:1

    2021-04-15 16:43:49.033880+0800 NetSchool[3863:1105334] subscribeNext:2

    2021-04-15 16:43:49.033913+0800 NetSchool[3863:1105334] subscribeNext:3

    2021-04-15 16:43:49.034021+0800 NetSchool[3863:1105334] subscribeNext:4

    2021-04-15 16:43:49.035297+0800 NetSchool[3863:1105334] signalA1完成

    2021-04-15 16:43:49.035822+0800 NetSchool[3863:1105334] subscribeNext:Error Domain=0 Code=0 "(null)"

    2021-04-15 16:43:49.035866+0800 NetSchool[3863:1105334] signalA1完成

    相关文章

      网友评论

          本文标题:RAC的Error操作

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