gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:01:11 28 4
gpt4 key购买 nike

我有 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 它们仅在所有信号完成时发送complete,flattenMap:combineLatest:reduce: 以处理每个的最新结果。

它有效,但我认为可能有更简洁、更好的方法来做到这一点。我该如何重写这部分以使其更简洁?

最佳答案

看看 - (RACSignal*)then:(RACSignal*(^)(void))block;我认为它非常适合您的情况。例如:

RACSignal* loginSignal = [[self authenticateWithUsername:username password:password] doNext:...];

RACSignal *resultSignal = [loginSignal then:^RACSignal* {
return [self fetchProfile];
}];
return resultSignal;

关于ios - 合并和减少,但在订阅下一个信号之前等待每个信号完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199243/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com