- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
我是 Mac OS X 开发新手。在 XCode/Cocoa 开发环境中,每个 Objective-C 项目都以 开始 #import 它工作正常,但我对 Cocoa.h 文件位置感到困惑。我的文件
所以我开始阅读这本书: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022 第 2 章解释了 MVC 设计模式,并给
我使用下面的代码来访问项目中的变量 appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; UIApplic
我想从我的表格 View 中拖动一行并将其放入 Mac OS X 10.6 中的任何其他 NSTextField 中,并放置一串文本。 拖放已经在我的应用程序中工作(在 NSTableView 和 N
如何在另一个窗口中加载 Nib ? 我尝试了 initWithWindowName, if (mmController == NULL) mmController = [[mainMenu a
其中一个类使用#import 。所以,在我的Podspec ,我包括 framework Cocoa 我的Podspec是 Pod::Spec.new do |s| s.name
有没有可以让我创建简单的条形图和折线图的框架? 最佳答案 有更新的开源Core Plot。 关于cocoa - cocoa :创建图表,我们在Stack Overflow上找到一个类似的问题: htt
如何从 SIMBL 插件获取主应用程序中的单例?当我尝试调用诸如 [ProcessControl sharedInstance] 之类的内容时,我收到一条错误,指出 ProcessControl 未定
我正在尝试从我的 cocoa 应用程序(通过使用 Mail.app)发送电子邮件中的一些文本。最初我尝试使用 HTML 发送格式正确的文本。但 mailto: URL 不支持 html 标签(即使在设
我正在创建一个应用程序,该应用程序必须与服务器数据交互,然后相应地显示数据库中的结果。我正在用 Cocoa 编写客户端应用程序。 示例:用户登录到 Web 应用程序。他们有一些提交网络报告的选项。选项
我想创建一个可以在多个应用程序中使用的框架(如 coreData、CoreAudio 等)。 任何人都可以发布此链接或教程... 最佳答案 尝试苹果的 Framework Programming Gu
我正在使用 [[NSFontManager sharedFontManager] collectionNames] 获取所有集合,但我看到一些未翻译的字符串(例如“com.apple.AllFonts
我刚刚开始我的 cocoa 教育,我有一个简单的问题,我看到单击一个单词并使用 mac 文本转语音功能的能力表明文本是自动内置的。 (即 - 对于 hello world 应用程序,您可以单击 hel
我需要在加密中实现盐,但要做到这一点,我需要将其存储为我需要创建的文件格式,以便稍后检索它进行解密。在加密方面我是个菜鸟。文件格式规范如下: 密文:密文长度;salt:盐的长度; 然后将密文和盐写出来
有没有办法在Cocoa中以任意的能力创建贝塞尔路径?例如,对于我的应用程序的一部分,我需要一个起伏的单元格。因此,我想使用 10 到 50 个不同的点绘制一条曲线,形成一个循环。这些点将随机波动。我认
我想将声音存储在用户默认值中。既然我们不能直接存储语音,那么存储它的最佳方式是什么?安装新语音后,使用数组 [NSSpeechSynthesizer availableVoices] 中的索引可能会有
我一直在寻找解决方案,但不知道是否可以执行以下操作: 我有一个drawRect方法,我想要做的是将图形元素(例如矩形和线条)添加到当前 View 而不刷新它。我曾经调用 setNeedsDisplay
美好的一天! 我正在为 Mac OS X(不是 iPhone)开发提醒软件。它应该一天一次显示一个窗口。具体时间没有规定,只是一次。我怎样才能做到呢。它会自行注册以在登录后启动。我已经尝试过 NSTi
我只是想知道是否可以创建具有层次结构的下拉或弹出菜单?我目前正在开发的应用程序跟踪作业、类(class)和主题。当用户创建作业时,他们需要能够从下拉列表中选择它所属的类(class),但我也不希望通过
是否有任何 Cocoa Widget 可以用来构建典型的(除了 Interface Builder 中的)GUI 构建器属性检查器,例如 RealBasic 或 Delphi? 是否有一个网站列出了其
我是一名优秀的程序员,十分优秀!