gpt4 book ai didi

ios - 在使用下一个事件之前,如何在 flattenMap block 中等待信号完成?

转载 作者:行者123 更新时间:2023-11-29 03:10:32 24 4
gpt4 key购买 nike

这是我的伪代码:

[[[@[ctx1,ctx2]rac_sequence]signalWithScheduler:RACScheduler.immediateScheduler]

flattenMap:^RACStream *(id ctx)
{
// first flatten map
return [RACSignal createSignal:^(id <RACSubscriber> subscriber)
{
[executeRequestAsynch
onDone:^{
[subscriber sendNext:ctx];
[subscriber sendCompleted];
}
]

}]
flattenMap:^RACStream *(id ctx) {

// second flattenMap
}];

}

现在这是我希望在订阅时发生的事情:

1)  ctx1 should get fed to the first flattenMap block
2) the Server Request "executeRequestAsynch" should be called
3) on completion of the serverRequest the second flattenMap should be called with ctx1
4) ctx2 should get fed to the first flattenMap block
5) the Server Request "executeRequestAsynch" should be called
6) on completion of the serverRequest the second flattenMap should be called with ctx1

但是这种情况发生了:

1)  ctx1 gets fed to the first flattenMap block
2) the Server Request "executeRequestAsynch" is called
3) ctx2 gets fed to the first flattenMap block
4) the Server Request "executeRequestAsynch" is called
5) on completion of the serverRequest the second flattenMap is called with ctx1
6) on completion of the serverRequest the second flattenMap is called with ctx2

如何实现第一个场景?


回答这个似乎可以解决问题!谢谢:输出是:

 Step 1
Step 1 child
Step 1 child child
Step 2
Step 2 child
Step 2 child child
done all

我仍然想知道 defer 在下面的 senario 中会实现什么

-(RACSignal*) executeRequestAsynch:(NSString*) ctx {
return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {
NSLog(@" %@ child",ctx);
[subscriber sendCompleted];
return nil;
}];

}

-(RACSignal*) executeRequestAsynch2:(NSString*) ctx {
return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {
NSLog(@" %@ child child",ctx);
[subscriber sendCompleted];
return nil;
}];

}

- (void)viewDidLoad
{
[super viewDidLoad];
RACSignal *contexts = [[@[ @"Step 1", @"Step 2" ]
rac_sequence] signalWithScheduler:RACScheduler.immediateScheduler];
RACSignal *ne = [[contexts map:^(id ctx) {
NSLog(@"%@",ctx);
return [[self executeRequestAsynch:ctx] concat: [self executeRequestAsynch2:ctx ]];}]concat] ;
[ne subscribeCompleted:^{
NSLog(@"done all");
}];
}

here is my final real solution

最佳答案

我不能 100% 确定我理解您想要实现的目标。我的解释是这样的:

RACSignal *contexts = [@[ ctx1, ctx2 ] things.rac_sequence signalWithScheduler:RACScheduler.immediateScheduler];
[[contexts map:^(id ctx) {
return [executeRequestAsynch concat:[RACSignal defer:^{
// Returns another signal involving ctx?
}]];
}] concat];

这会将每个上下文映射到 executeRequestAsynch,然后将它们连接起来,以便串行完成。

是这样还是我误解了你的意思?

关于ios - 在使用下一个事件之前,如何在 flattenMap block 中等待信号完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22298551/

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