RACCommand error信號(hào)回調(diào)隨筆

ViewModel創(chuàng)建RACCommand:

-(RACCommand *)command{

? ? if(!_command){

? ? ? ? _command =[[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id? _Nullable input){

? ? ? ? ? ? return[self signal];

? ? ? ? }];

? ? }

? ? return _command;

}

-(RACSignal *)signal{

? ? return[RACSignal createSignal:^RACDisposable * _Nullable(id? _Nonnull subscriber){

? ? ? ? [[RequestManager sharedManager]showHud:YES successBlock:^(NSDictionary *response){

? ? ? ? ? ? [subscriber sendNext:Model];

? ? ? ? ? ? [subscriber sendCompleted];

? ? ? ? } failureBlock:^(NSError *error){

? ? ? ? ? ? [subscriber sendError:error];

? ? ? ? ? ? [subscriber sendCompleted];

? ? ? ? }];

? ? ? ? return nil;

? ? }];

}

在View層調(diào)用Command:

?[self.viewModel.couponDetailCommand.executionSignals.switchToLatest subscribeNext:^(id? _Nullable x){

? ? ? ? [self showCouponDetailView];

? ? }error:^(NSError * _Nullable error){

? ? ? ? NSLog()

? ? }];

?[self.viewModel.couponDetailCommand execute:@1];

這種方式viewModel中sendError得話是收不到回調(diào)。

解決方案:

一、更改為:

? ? [[self.viewModel.couponDetailCommand execute:@{}]subscribeNext:^(id? _Nullable x){

? ? ? ? [self showCouponDetailView];

? ? }error:^(NSError * _Nullable error){

? ? ? ? [self showNormalView];

? ? }];

二、使用Command的error信號(hào)

[self.viewModel.couponDetailCommand.executionSignals.switchToLatest subscribeNext:^(id? _Nullable x){

? ? ? ? //請(qǐng)求成功

? ? }];

?[self.viewModel.couponDetailCommand.errors subscribeNext:^(NSError * _Nullable x){

? ? ? ? //請(qǐng)求失敗

?}];

三、使用materialize和dematerialize

materialize將所有的信號(hào)封裝成event一并回調(diào),但會(huì)導(dǎo)致Command對(duì)象中error、executing等信號(hào)無(wú)回調(diào),不建議使用

-(RACCommand *)couponDetailCommand{

? ? if(!_couponDetailCommand){


? ? ? ? _couponDetailCommand =[[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id? _Nullable input){

? ? ? ? ? ? return[[self signal]materialize];

? ? ? ? }];

? ? }

? ? return _couponDetailCommand;

}

[self.viewModel.couponDetailCommand.executionSignals subscribeNext:^(RACSignal *execution){

? ? ? ? [[[execution dematerialize]deliverOn:[RACScheduler mainThreadScheduler]]subscribeError:^(NSError *error){

? ? ? ? ? ? NSLog(@"發(fā)生錯(cuò)誤");

? ? ? ? } completed:^{

? ? ? ? ? ? NSLog(@"完成");

? ? ? ? }];

?}];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容