在订阅下一个信号之前,合并并减少但等待每个信号完成

时间:2015-05-12 18:54:43

标签: ios objective-c reactive-cocoa

我有2个网络信号,第一个需要在下一个网络信号启动之前完成,因为我需要为第二个请求发送accessToken,并在第一个请求中获得该信号。然后我想将每个步骤的值减少为单个对象。我猜combineLatest:reduce:订阅了它们,与等待完成信号无关。

我现在有这个:

- (RACSignal *)loginWithEmail:(NSString *)email password:(NSString *)password
{
    @weakify(self);
    RACSignal *authSignal = [[self requestTokensWithUsername:email password:password]  
        doNext:^(Authentication *auth) {
            @strongify(self);
            self.authentication = auth;  
        }];

    RACSignal *profileSignal = [self fetchProfile];
    NSArray *orderedSignals = @[ authSignal, profileSignal ];
    RACSignal *userSignal =
        [RACSignal combineLatest:orderedSignals
                          reduce:^id(Authentication *auth, Profile *profile) {
                              NSLog(@"combine: %@, %@", auth, profile);
                              return [User userWithAuth:auth profile:profile history:nil];
                          }];

    return [[[RACSignal concat:orderedSignals.rac_sequence] collect]
        flattenMap:^RACStream * (id value) {                
            return userSignal;
        }];
}

为了确保它们按顺序完成,我返回一个信号,我首先concat:信号,然后collect它们仅在所有信号完成时发送完成,{{1} } flattenMap:来处理每个的最新结果。

它有效但我认为可能有更简洁和更好的方法来做到这一点。我怎么可能重写这部分以使它更简洁呢?

2 个答案:

答案 0 :(得分:2)

看看=FILTER(Sheet1!A1:C,ARRAYFORMULA(Sheet1!A1:C1="achievement")) 我认为它非常适合你的情况。 例如:

- (RACSignal*)then:(RACSignal*(^)(void))block;

答案 1 :(得分:1)

FWIW,我简化了我的代码并对结果感到满意。我将它留在这里以供将来参考。

- (RACSignal *)loginWithEmail:(NSString *)email password:(NSString *)password
{
    @weakify(self);
    RACSignal *authSignal = [[self requestTokensWithUsername:email password:password]  
        doNext:^(Authentication *auth) {
            @strongify(self);
            self.authentication = auth;  
        }];

    RACSignal *profileSignal = [self fetchProfile];
    RACSignal *historySignal = [self fetchHistory];
    RACSignal *balanceSignal = [self fetchBalanceDetails];

    NSArray *orderedSignals = @[ authSignal, balanceSignal, profileSignal, historySignal ];

    return [[[RACSignal concat:orderedSignals.rac_sequence]  
        collect]                                              
        map:^id(NSArray *parameters) {

            return [User userWithAuth:parameters[0]
                              balance:parameters[1]
                              profile:parameters[2]
                              history:parameters[3]];
        }];
}