【发布时间】:2015-05-12 18:54:43
【问题描述】:
我有 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 它们仅在所有信号完成后才发送完成,然后将 flattenMap: 发送到 combineLatest:reduce:处理每个的最新结果。
它有效,但我认为可能有更简洁和更好的方法来做到这一点。我该如何重写这部分以使其更简洁?
【问题讨论】:
标签: ios objective-c reactive-cocoa