This is the solution from my last question:
Since I am new to reactiveCocoa I am a little unsure if this is the right way to go?
Basicly I want to get my dependent network Requests sent serialized one after the other.
They form a tree so the parent is sent first and then any children and then the next parent:
After doing some testing the underneath code seems to do exactly what I want: Could somebody tell me if I am using reactiveCocoa the right way? Could I run into deadlocks?
#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
OUTPUT IS :
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
After considering the suggested changes I arrived at the following code :
#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");
}];
}