我正在尝试将我的iOS应用程序重构为ReactiveCocoa和ReactiveViewModel,并且努力尝试制定一些最佳实践。使用ReactiveCocoa重试命令
我将这个归结为一个简单的用例 - 我推动视图控制器加载一些数据并将其推入到一个表视图中。如果因任何原因终端呼叫失败,我想用重试按钮在屏幕上显示一个视图。
我目前有这个工作,但它看起来有点肮脏。我觉得必须有更好的方式 - 我是否正确地做到了这一点?
在我的ViewModel的init
方法中,我创建了我的命令,在ViewModel变为活动状态时立即调用它。
// create the command to load the data
@weakify(self);
self.loadStationsCommand = [[RACCommand alloc] initWithSignalBlock:^(RACSignal *(id input) {
@strongify(self);
return [RACSignal createSignal:^(RACDisposable *(id<RACSubscriber subscriber) {
// load data from my API endpoint
...
BOOL succeeded = ...;
if (succeeded) {
[subscriber sendNext:nil];
[subscriber sendCompleted:nil];
} else {
// failed
[subscriber sendError:nil];
}
return nil;
}
}];
// start the command when the ViewModel's ready
[self.didBecomeActiveSignal subscribeNext:^(id x) {
@strongify(self);
[self.loadStationsCommand execute:nil];
}];
在我的UIViewController,我通过订阅的指令 -
[self.viewModel.loadStationsCommand.executionSignals subscribeNext:^(RACSignal *loadStationsSignal) {
[loadStationsSignal subscribeNext:^(id x) {
// great, we got the data, reload the table view.
@strongify(self);
[self.tableView reloadData];
} error:^(NSError *error) {
// THIS NEVER GETS CALLED?!
}];
}];
[self.viewModel.loadStationsCommand.errors subscribeNext:^(id x) {
// I actually get my error here.
// Show view/popup to retry the endpoint.
// I can do this via [self.viewModel.loadStationsCommand execute:nil]; which seems a bit dirty too.
}];
我必须有一些误解如何RACCommand
的作品,或者至少是我觉得我没有做这尽可能干净。
为什么我的loadStationsSignal
上的错误块没有被调用?为什么我需要订阅executionCommand.errors
而不是?
有没有更好的方法?