gpt4 book ai didi

ios - 正在寻找用响应式(Reactive) cocoa 链接网络请求的依赖树的最优雅的方法?

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

This is the solution from my last question:

由于我是 reactiveCocoa 的新手,所以我有点不确定这是否是正确的方法?

基本上,我想让我的依赖网络请求一个接一个地序列化。

它们形成了一棵树,因此先发送父节点,然后发送任何子节点,最后发送下一个父节点:

在做了一些测试之后,下面的代码似乎完全符合我的要求:有人可以告诉我我是否以正确的方式使用 reactiveCocoa?我会遇到死锁吗?

#import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface ViewController () {
dispatch_queue_t backgroundQueue;
}
@end

@implementation ViewController
/**
simulating network requests
*/

-(RACSignal*) executeRequestAsynch:(NSString*) ctx {
return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

dispatch_async(backgroundQueue, ^(void) {
NSLog(@" saving depending %@ ",ctx);
[subscriber sendCompleted];
});

return nil;
}];
}
/**
simulating network requests
*/
-(RACSignal*) executeRequestAsynchChildStep:(NSString*) ctx withParent:(NSString *) parent {
return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

dispatch_async(backgroundQueue, ^(void) {
NSLog(@" saving depending ChildStep %@ of parent step:%@",ctx,parent);
[subscriber sendCompleted];
});

return nil;
}];

}

-(RACSignal*) executeRequestAsynch2:(NSString*) parent {
RACSignal *contexts = [[@[ @"ChildStep 1", @"ChildStep 2",@"ChildStep 3", @"ChildStep 4"] rac_sequence] signalWithScheduler:RACScheduler.immediateScheduler];
contexts = [contexts map:^(id ctx) {
return [self executeRequestAsynchChildStep:ctx withParent:parent];
}];
return [contexts concat];

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


@end

输出是:

2014-03-11 15:06:23.361 test[2470:70b] iterating map  Step 1
2014-03-11 15:06:23.362 test[2470:70b] iterating map Step 2
2014-03-11 15:06:23.362 test[2470:1303] saving depending Step 1
2014-03-11 15:06:23.363 test[2470:70b] iterating map Step 3
2014-03-11 15:06:23.363 test[2470:1303] saving depending ChildStep ChildStep 1 of parent step:Step 1
2014-03-11 15:06:23.363 test[2470:70b] iterating map Step 4
2014-03-11 15:06:23.363 test[2470:70b] iterating map Step 5
2014-03-11 15:06:23.363 test[2470:1303] saving depending ChildStep ChildStep 2 of parent step:Step 1
2014-03-11 15:06:23.364 test[2470:70b] iterating map Step 6
2014-03-11 15:06:23.364 test[2470:1303] saving depending ChildStep ChildStep 3 of parent step:Step 1
2014-03-11 15:06:23.364 test[2470:1303] saving depending ChildStep ChildStep 4 of parent step:Step 1
2014-03-11 15:06:23.365 test[2470:1303] saving depending Step 2
2014-03-11 15:06:23.365 test[2470:1303] saving depending ChildStep ChildStep 1 of parent step:Step 2
2014-03-11 15:06:23.365 test[2470:1303] saving depending ChildStep ChildStep 2 of parent step:Step 2
2014-03-11 15:06:23.370 test[2470:1303] saving depending ChildStep ChildStep 3 of parent step:Step 2
2014-03-11 15:06:23.370 test[2470:1303] saving depending ChildStep ChildStep 4 of parent step:Step 2
2014-03-11 15:06:23.370 test[2470:1303] saving depending Step 3
2014-03-11 15:06:23.371 test[2470:1303] saving depending ChildStep ChildStep 1 of parent step:Step 3
2014-03-11 15:06:23.371 test[2470:1303] saving depending ChildStep ChildStep 2 of parent step:Step 3
2014-03-11 15:06:23.372 test[2470:1303] saving depending ChildStep ChildStep 3 of parent step:Step 3
2014-03-11 15:06:23.372 test[2470:1303] saving depending ChildStep ChildStep 4 of parent step:Step 3
2014-03-11 15:06:23.372 test[2470:3803] saving depending Step 4
2014-03-11 15:06:23.373 test[2470:3503] saving depending ChildStep ChildStep 1 of parent step:Step 4
2014-03-11 15:06:23.373 test[2470:3803] saving depending ChildStep ChildStep 2 of parent step:Step 4
2014-03-11 15:06:23.373 test[2470:3503] saving depending ChildStep ChildStep 3 of parent step:Step 4
2014-03-11 15:06:23.401 test[2470:3503] saving depending ChildStep ChildStep 4 of parent step:Step 4
2014-03-11 15:06:23.402 test[2470:3503] saving depending Step 5
2014-03-11 15:06:23.402 test[2470:3503] saving depending ChildStep ChildStep 1 of parent step:Step 5
2014-03-11 15:06:23.402 test[2470:3503] saving depending ChildStep ChildStep 2 of parent step:Step 5
2014-03-11 15:06:23.403 test[2470:3503] saving depending ChildStep ChildStep 3 of parent step:Step 5
2014-03-11 15:06:23.403 test[2470:3503] saving depending ChildStep ChildStep 4 of parent step:Step 5
2014-03-11 15:06:23.404 test[2470:3503] saving depending Step 6
2014-03-11 15:06:23.404 test[2470:3503] saving depending ChildStep ChildStep 1 of parent step:Step 6
2014-03-11 15:06:23.405 test[2470:3503] saving depending ChildStep ChildStep 2 of parent step:Step 6
2014-03-11 15:06:23.405 test[2470:3503] saving depending ChildStep ChildStep 3 of parent step:Step 6
2014-03-11 15:06:23.405 test[2470:3503] saving depending ChildStep ChildStep 4 of parent step:Step 6
2014-03-11 15:06:23.406 test[2470:3503] done all

在考虑了建议的更改后,我得出了以下代码:

    #import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface ViewController () {
dispatch_queue_t backgroundQueue;
}
@end

@implementation ViewController
/**
simulating network requests
*/
-(RACSignal*) executeRequestAsynch:(NSString*) ctx {
return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

dispatch_async(backgroundQueue, ^(void) {
//simulating URLConnnection network requests with completion handler
NSLog(@" saving depending %@ ",ctx);
[subscriber sendCompleted];
});
return nil;
}];
}
/**
simulating network requests
*/
-(RACSignal*) executeRequestAsynchChildStep:(NSString*) ctx withParent:(NSString *) parent {
return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

dispatch_async(backgroundQueue, ^(void) {
//simulating URLConnnection network requests with completion handler
NSLog(@" saving depending ChildStep %@ of parent step:%@",ctx,parent);
[subscriber sendCompleted];
});
return nil;
}];

}
-(RACSignal*) executeRequestAsynch2:(NSString*) parent {
RACSequence *contexts = [@[ @"ChildStep 1", @"ChildStep 2",@"ChildStep 3", @"ChildStep 4"] rac_sequence];
contexts = [contexts map:^(id ctx) {
return [self executeRequestAsynchChildStep:ctx withParent:parent];
}];
return [RACSignal concat:contexts];

}

- (void)viewDidLoad
{
[super viewDidLoad];
backgroundQueue = dispatch_queue_create("blah", NULL);
RACSignal *contexts = [[@[ @"Step 1", @"Step 2",@"Step 3", @"Step 4"] rac_sequence] signal];
RACSignal *ne = [[contexts map:^(id ctx) {
NSLog(@"iterating map %@",ctx);
return [[self executeRequestAsynch:ctx] concat: [self executeRequestAsynch2:ctx ]];
}]concat] ;
[ne subscribeCompleted:^{
NSLog(@"done all");
}];
}

最佳答案

总的来说,代码看起来就像您希望它做的那样。我认为没有任何理由担心僵局。您是否认为某个特定领域可能会陷入僵局?

我可以给你一些关于代码的建议。

你已经清楚地看到 ReactiveCocoa 有一个调度器(RACScheduler)的概念,这个类可以让你取代调度队列的命令式使用。上面的这个信号:

return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
dispatch_async(backgroundQueue, ^{
NSLog(@" saving depending ChildStep %@ of parent step:%@", ctx, parent);
[subscriber sendCompleted];
});
return nil;
}];

可以使用 -subscribeOn: 重写:

RACScheduler *backgroundScheduler = [[RACScheduler alloc] initWithName:nil targetQueue:backgroundQueue];

return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
NSLog(@" saving depending ChildStep %@ of parent step:%@", ctx, parent);
[subscriber sendCompleted];
return nil;
}] subscribeOn:backgroundScheduler];

接下来,可以简化您使用序列的方式。您可以将序列传递给 +[RACSignal concat:],而不是将序列转换为信号。例如,而不是:

RACSignal *contexts = [[@[@"ChildStep 1", … @"ChildStep N"] rac_sequence] signalWithScheduler:RACScheduler.immediateScheduler];
contexts = [contexts map:^(id ctx) {
return [self executeRequestAsynchChildStep:ctx withParent:parent];
}];
return [contexts concat];

您可以放弃使用-signalWithScheduler:,并应用+concat::

RACSequence *contexts = [@[@"ChildStep 1", … @"ChildStep N"] rac_sequence];
contexts = [contexts map:^(id ctx) {
return [self executeRequestAsynchChildStep:ctx withParent:parent];
}];
return [RACSignal concat:contexts];

希望对您有所帮助。

附言。通过following the ReactiveCocoa discussions可以学到很多东西.

关于ios - 正在寻找用响应式(Reactive) cocoa 链接网络请求的依赖树的最优雅的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22328557/

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