ios - How to send error in signal chain -
i have signal contains integer values. value of -1 indicates invalid result, i'd to, instead of passing -1 along value, send error. way subscribes b receive valid integers through subscribenext: , errors through subscribeerror:.
i think know how racsubject:
racsequence *a = [@[ @(2), @(6), @(5), @(-1), @(4) ] rac_sequence]; racsubject *b = [racsubject subject]; [a subscribenext:^(nsnumber *val) { if ( [val integervalue] == -1 ) { [b senderror:[nserror errorwithdomain:@"mydomain" code:0 userinfo:nil]]; } else { [b sendnext:val]; } } error:^(nserror *error) { [b senderror:error]; } completed:^{ [b sendcompleted]; }];
i'm wondering if there's more "inline" way along lines of:
racsequence *a = [@[ @(2), @(6), @(5), @(-1), @(4) ] rac_sequence]; racsignal *b = [a filter:^bool(id val) { if ( [val integervalue] == -1 ) { //fixme: send error b's subscriber(s) return no; } else { return yes; } }
the primary method using -flattenmap:
, similar how you've written -filter:
above. using example:
racsignal *b = [a flattenmap:^(nsnumber *number) { if (number.intvalue == -1) { return [racsignal error:[nserror errorwithdomain:@"mydomain" code:0 userinfo:nil]]; } else { return [racsignal return:number]; } }];
update
alternatively, using newer -try:
operator:
racsignal *b = [a try:^(nsnumber *number, nserror **error) { if (number.intvalue == -1) { *error = [nserror errorwithdomain:@"mydomain" code:0 userinfo:nil]; return no; } return yes; }];
Comments
Post a Comment