如何合并两个Reactive Cocoa命令?

时间:2016-12-21 17:32:49

标签: ios objective-c reactive-cocoa

我正在进行2次网络通话(案例详情和留言详情)。我想合并两个RACCommands,以便它们都完成,然后我调用nextMethod()。

以下是两个彼此独立工作的电话:

@weakify(self);
[[self.loadCaseCommand.executionSignals switchToLatest] subscribeNext:^(MZACaseDetails *caseDetails) {
    @strongify(self);
    self.caseDetails = caseDetails;
    self.watcherAccessLevel = caseDetails.watcherAccessLevel;
    self.mutableCaseDetails = (MZAMutableCaseDetails *)[self.caseDetails mutableCopy];

    [self createViewModels];
    [(RACSubject *)self.contentDidChangeSignal sendNext:nil];
}];

[[self.loadMessagesCommand.executionSignals switchToLatest] subscribeNext:^(MZAMessage *messageDetails) {
    @strongify(self);
    self.messageDetails = messageDetails;
}];

1 个答案:

答案 0 :(得分:0)

如果你有两个或更多的RACSignal并希望将它们组合成一个sendNext:当所有输入sendNext:和sendError:当一个发送错误时你应该使用zip操作符。

http://reactivex.io/documentation/operators/zip.html

在反应性可可中它看起来像

RACSignal* signal1 = [self.loadCaseCommand.executionSignals switchToLatest];
RACSignal* signal2 = [self.loadMessagesCommand.executionSignals switchToLatest];

[RACSignal zip:@[signal1, signal2]] subscribeNext:^(RACTuple *data)
{
    RACTupleUnpack(MZACaseDetails *caseDetails, MZAMessage *messageDetails) = data;
    //...
}