我对 ReactiveCocoa 比较陌生,想知道如何将一系列 REST GET 调用链接在一起,以便它们按顺序执行。如果其中一个调用出错,那么整个过程将回滚。
所以我正在使用 pod 'AFNetworking-RACExtensions', '0.1.1' 我有一个 NSArray 的信号。这些信号中的大多数看起来像这样:
- (RACSignal *) form
{
@weakify(self);
RACSubject *repSubject = [RACSubject subject];
[[ServiceClient getForm] subscribeNext:^(RACTuple *jsonTuple) {
if ([jsonTuple second])
{
// create core data objects here
[repSubject sendNext: nil];
[repSubject sendCompleted];
}
} error:^(NSError *error) {
[repSubject sendError: error];
}];
return repSubject;
}
所以像这样的大量信号在 NSArray 中。我希望按照它们在数组中出现的顺序处理这些调用,并拥有一个共享的错误处理程序和完成块。我认为我在不使用 nsarray 方面取得了成功,并且代码如下:
@weakify(self);
[[[[[[self form] flattenMap:^(id value) {
// perform your custom business logic
@strongify(self);
return [self signal2];
}] flattenMap:^(id value) {
// perform your custom business logic
@strongify(self);
return [self signal3];
}] flattenMap:^(id value) {
// perform your custom business logic
@strongify(self);
return [self signal4];
}] flattenMap:^(id value) {
// perform your custom business logic
@strongify(self);
return [self signal5];
}] subscribeError:^(NSError *error) {
@strongify(self);
[self handleError: error];
} completed:^{
// Successful Full Sync
// post notification
}];
如何使用 NSArray 信号来完成所有这些操作,同时仍然能够使用 subscribeError 和已完成的块?
我假设它是这样的:
@weakify(self);
[[[array.rac_sequence flattenMap:^RACStream *(id value) {
// dunno what to do
}] subscribeError:^(NSError *error) {
@strongify(self);
[self handleError: error];
} completed:^{
// Successful Full Sync
// post notification
}];